Android消息提示框Toast

Android消息提示框Toast
Toast是Android中一种简易的消息提示框。和Dialog不一样的是,Toast是没有焦点的,toast提示框不能被用户点击,而且Toast显示的时间有限,toast会根据用户设置的显示时间后自动消失。
创建Toast的方法总共有2种:
1.Toast.makeText(Context context, (CharSequence text)/( int resId), int duration)
参数:context是指上下文对象,通常是当前的Activity,text是指自己写的消息内容,resId指显示内容引用Resource的那条数据,duration指Toast的显示时间,Toast默认有LENGTH_SHORT和LENGTH_LONG两常量,分别表示时间长和短,也可以自定义时间,如:5000表示显示5秒。用此方法获得Toast对象之后调用.show()方法即可显示消息。
2.自定义Toast,通过Toast toast = new Toast(Context context)获得Toast对象,调用.show()方法即可显示消息。
toast.setDuration(duration)设置显示时间,
toast.setGravity(gravity, xOffset, yOffset)设置显示位置,三个参数分别表示(起点位置,水平位移,垂直位移),
toast.setMargin(float horizontalMargin, float verticalMargin)以横向和纵向的百分比设置显示位置,参数均为float类型(水平位移正右负左,竖直位移正上负下),
toast.setText(text)设置消息内容,
toast.setView(view)设置Toast显示的视图组件。
 
实例:
main.xml
[html] view plain copy
 
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical"  
  7.     android:padding="10dp" >  
  8.   
  9.     <Button  
  10.         android:id="@+id/simpleToast"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="默认" >  
  14.     </Button>  
  15.   
  16.     <Button  
  17.         android:id="@+id/simpleToastWithCustomPosition"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="自定义显示位置" >  
  21.     </Button>  
  22.   
  23.     <Button  
  24.         android:id="@+id/imageToast"  
  25.         android:layout_width="fill_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:text="带图片" >  
  28.     </Button>  
  29.   
  30.     <Button  
  31.         android:id="@+id/customToast"  
  32.         android:layout_width="fill_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="完全自定义" >  
  35.     </Button>  
  36.   
  37.     <Button  
  38.         android:id="@+id/runToastFromOtherThread"  
  39.         android:layout_width="fill_parent"  
  40.         android:layout_height="wrap_content"  
  41.         android:text="其他线程" >  
  42.     </Button>  
  43.   
  44. </LinearLayout>  

custom.xml
[html] view plain copy
 
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/toastLayout"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_margin="1dp"  
  12.         android:background="#333399"  
  13.         android:gravity="center"  
  14.         android:text="ToastTitle"  
  15.         android:textColor="#ffffff" />  
  16.   
  17.     <LinearLayout  
  18.         android:id="@+id/toastContent"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_marginBottom="1dp"  
  22.         android:layout_marginLeft="1dp"  
  23.         android:layout_marginRight="1dp"  
  24.         android:background="#3366ff"  
  25.         android:orientation="vertical"  
  26.         android:padding="15dp" >  
  27.   
  28.         <ImageView  
  29.             android:id="@+id/customImageToast"  
  30.             android:layout_width="wrap_content"  
  31.             android:layout_height="wrap_content"  
  32.             android:layout_gravity="center" />  
  33.   
  34.         <TextView  
  35.             android:id="@+id/customTextToast"  
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             android:gravity="center"  
  39.             android:paddingLeft="10dp"  
  40.             android:paddingRight="10dp" />  
  41.     </LinearLayout>  
  42.   
  43. </LinearLayout>  

MainActivity.java
[java] view plain copy
 
 print?
  1. public class MainActivity extends Activity implements OnClickListener {  
  2.   
  3.     private Button simpleToastBtn, simpleToastWithCustomPositionBtn,  
  4.             imageToastBtn, customToastBtn, runToastFromOtherThreadBtn;  
  5.     Handler handler = new Handler();  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main);  
  11.         simpleToastBtn = (Button) findViewById(R.id.simpleToast);  
  12.         simpleToastWithCustomPositionBtn = (Button) findViewById(R.id.simpleToastWithCustomPosition);  
  13.         imageToastBtn = (Button) findViewById(R.id.imageToast);  
  14.         customToastBtn = (Button) findViewById(R.id.customToast);  
  15.         runToastFromOtherThreadBtn = (Button) findViewById(R.id.runToastFromOtherThread);  
  16.   
  17.         simpleToastBtn.setOnClickListener(this);  
  18.         simpleToastWithCustomPositionBtn.setOnClickListener(this);  
  19.         imageToastBtn.setOnClickListener(this);  
  20.         customToastBtn.setOnClickListener(this);  
  21.         runToastFromOtherThreadBtn.setOnClickListener(this);  
  22.     }  
  23.   
  24.     @Override  
  25.     public void onClick(View v) {  
  26.         Toast toast = null;  
  27.         switch (v.getId()) {  
  28.         case R.id.simpleToast:  
  29.             Toast.makeText(MainActivity.this, "默认Toast样式", Toast.LENGTH_LONG)  
  30.                     .show();  
  31.             break;  
  32.   
  33.         case R.id.simpleToastWithCustomPosition:  
  34.             toast = Toast.makeText(MainActivity.this, "自定义位置Toast",  
  35.                     Toast.LENGTH_LONG);  
  36.             toast.setGravity(Gravity.CENTER, 0, 0);  
  37.             toast.show();  
  38.             break;  
  39.   
  40.         case R.id.imageToast:  
  41.             toast = Toast.makeText(MainActivity.this, "带图片的Toast",  
  42.                     Toast.LENGTH_LONG);  
  43.             LinearLayout layout = (LinearLayout) toast.getView();  
  44.             ImageView image = new ImageView(MainActivity.this);  
  45.             image.setImageResource(R.drawable.ic_launcher);  
  46.             layout.addView(image, 0);  
  47.             toast.show();  
  48.             break;  
  49.   
  50.         case R.id.customToast:  
  51.             LayoutInflater inflater = getLayoutInflater();  
  52.             View view = inflater.inflate(R.layout.custom, null);  
  53.   
  54.             ImageView customImage = (ImageView) view  
  55.                     .findViewById(R.id.customImageToast);  
  56.             TextView customText = (TextView) view  
  57.                     .findViewById(R.id.customTextToast);  
  58.             customImage.setImageResource(R.drawable.ic_launcher);  
  59.             customText.setText("完全自定义Toast");  
  60.   
  61.             toast = new Toast(MainActivity.this);  
  62.             toast.setGravity(Gravity.RIGHT | Gravity.TOP, 30, 30);  
  63.             toast.setDuration(Toast.LENGTH_LONG);  
  64.             toast.setView(view);  
  65.             toast.show();  
  66.             break;  
  67.   
  68.         case R.id.runToastFromOtherThread:  
  69.             new Thread(new Runnable() {  
  70.                 public void run() {  
  71.                     showToast();  
  72.                 }  
  73.             }).start();  
  74.             break;  
  75.         }  
  76.     }  
  77.   
  78.     public void showToast() {  
  79.         handler.post(new Runnable() {  
  80.             @Override  
  81.             public void run() {  
  82.                 Toast.makeText(MainActivity.this, "我来自其他线程", Toast.LENGTH_LONG)  
  83.                         .show();  
  84.             }  
  85.         });  
  86.     }  
  87. }  
原文地址:https://www.cnblogs.com/susanws/p/5411406.html