Toast常用于向用户展示一些帮助与提示,这里简单列举了toast的设置显示位置,显示image,自定义与多线程的使用!
package com.example.toastdemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
class ToastUtil {
public void showToast(String tips) {
Toast.makeText(MainActivity.this, tips, Toast.LENGTH_SHORT).show();
}
/**
* set toast show position
*/
public void showToast(String tips, int gravity) {
Toast toast = Toast.makeText(MainActivity.this, tips, Toast.LENGTH_SHORT);
toast.setGravity(gravity, 0, 0);
toast.show();
}
/**
* @param tips: show message
* @param gravity: toast show position
* @param imageId: image id
*/
public void showToast(String tips, int gravity, int imageId) {
Toast toast = Toast.makeText(MainActivity.this, tips, Toast.LENGTH_SHORT);
toast.setGravity(gravity, 0, 0);
LinearLayout linear = (LinearLayout)(toast.getView());
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(imageId);
linear.addView(imageView);
toast.show();
}
/**
* show message in non-ui thread
* @param tips:
* warnning: handler object obtain by ui thread
*/
public void showToast(final String tips, Handler handler) {
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, tips, Toast.LENGTH_SHORT).show();
}
});
}
public void customToast(int layoutId){
View view = LayoutInflater.from(MainActivity.this).inflate(layoutId, null);
Toast toast = new Toast(MainActivity.this);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(view); //set custom view
toast.show();
}
};
private final static String s_msg = "This is a toast test !";
private ToastUtil toastUtil = new ToastUtil();
private Handler handler = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn_toast_common:
toastUtil.showToast(s_msg);
break;
case R.id.btn_toast_gravity:
toastUtil.showToast(s_msg, Gravity.CENTER);
break;
case R.id.btn_toast_image:
toastUtil.showToast(s_msg, Gravity.CENTER, R.drawable.icon);
break;
case R.id.btn_toast_custom:
toastUtil.customToast(R.layout.toast_custom);
break;
case R.id.btn_toast_thread:
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
toastUtil.showToast(s_msg, handler);
}
}).start();
break;
default:
break;
}
}
}
整个工程的布局文件(main_layout.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button android:id="@+id/btn_toast_common"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="toast_common"
android:onClick="onClick"/>
<Button android:id="@+id/btn_toast_gravity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="toast_gravity"
android:onClick="onClick"/>
<Button android:id="@+id/btn_toast_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="toast_image"
android:onClick="onClick"/>
<Button android:id="@+id/btn_toast_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="toast_custom"
android:onClick="onClick"/>
<Button android:id="@+id/btn_toast_thread"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="toast_thread"
android:onClick="onClick"/>
</LinearLayout>
toast的自定义布局文件(toast_custom.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/custom_toast_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/custom_toast_view"
android:background="@drawable/custom_toast_view"
android:gravity="center"/>
</LinearLayout>
custom_toast_view.xml定义:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#ff0000e0" />
<stroke android:width="0.5dp" android:color="#ffff0000"/>
<padding android:top="20dp" android:bottom="20dp"
android:left="10dp" android:right="10dp"/>
<gradient android:startColor="#ffff0000" android:endColor="#80ff22ff"/>
</shape>
自定义效果如下:
下载地址请点击这里