• Android手机开发之Toast通知的用法
  • 发布于 2个月前
  • 258 热度
    0 评论

OAST在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。

1.默认用法

  1. Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT).show();  

2.Fragment中的用法

  1. Toast.makeText(getActivity(),"网络连接错误,请检察网络设置", Toast.LENGTH_LONG).show();  

3.自定义显示位置效果

  1. toast = Toast.makeText(getApplicationContext(), "自定义位置Toast", Toast.LENGTH_LONG);  
  2. toast.setGravity(Gravity.CENTER, 0, 0);  
  3. toast.show();  

4.带图片效果

  1. toast = Toast.makeText(getApplicationContext(), "带图片的Toast", Toast.LENGTH_LONG);  
  2. toast.setGravity(Gravity.CENTER, 0, 0);  
  3. LinearLayout toastView = (LinearLayout) toast.getView();  
  4. ImageView imageCodeProject = new ImageView(getApplicationContext());  
  5. imageCodeProject.setImageResource(R.drawable.icon);  
  6. toastView.addView(imageCodeProject, 0);  
  7. toast.show();  

5.完全自定义效果

  1. LayoutInflater inflater = getLayoutInflater();  
  2. View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast));  
  3. ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);  
  4. image.setImageResource(R.drawable.icon);  
  5. TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);  
  6. title.setText("Attention");  
  7. TextView text = (TextView) layout.findViewById(R.id.tvTextToast);  
  8. text.setText("完全自定义Toast");  
  9. toast = new Toast(getApplicationContext());  
  10. toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);  
  11. toast.setDuration(Toast.LENGTH_LONG);  
  12. toast.setView(layout);  
  13. toast.show();  

6.其他线程

Main.java打码
  1. package com.wjq.toast;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.view.Gravity;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.ImageView;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15.   
  16. public class Main extends Activity implements OnClickListener {  
  17.     Handler handler = new Handler();  
  18.   
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.   
  24.         findViewById(R.id.btnSimpleToast).setOnClickListener(this);  
  25.         findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(this);  
  26.         findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);  
  27.         findViewById(R.id.btnCustomToast).setOnClickListener(this);  
  28.         findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);  
  29.   
  30.     }  
  31.   
  32.     public void showToast() {  
  33.         handler.post(new Runnable() {  
  34.   
  35.             @Override  
  36.             public void run() {  
  37.                 Toast.makeText(getApplicationContext(), "我来自其他线程!",Toast.LENGTH_SHORT).show();  
  38.             }  
  39.         });  
  40.     }  
  41.   
  42.     @Override  
  43.     public void onClick(View v) {  
  44.         Toast toast = null;  
  45.         switch (v.getId()) {  
  46.         case R.id.btnSimpleToast:  
  47.             Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT).show();  
  48.             break;  
  49.         case R.id.btnSimpleToastWithCustomPosition:  
  50.             toast = Toast.makeText(getApplicationContext(), "自定义位置Toast",Toast.LENGTH_LONG);  
  51.             toast.setGravity(Gravity.CENTER, 0, 0);  
  52.             toast.show();  
  53.             break;  
  54.         case R.id.btnSimpleToastWithImage:  
  55.             toast = Toast.makeText(getApplicationContext(), "带图片的Toast",Toast.LENGTH_LONG);  
  56.             toast.setGravity(Gravity.CENTER, 0, 0);  
  57.             LinearLayout toastView = (LinearLayout) toast.getView();  
  58.             ImageView imageCodeProject = new ImageView(getApplicationContext());  
  59.             imageCodeProject.setImageResource(R.drawable.icon);  
  60.             toastView.addView(imageCodeProject, 0);  
  61.             toast.show();  
  62.             break;  
  63.         case R.id.btnCustomToast:  
  64.             LayoutInflater inflater = getLayoutInflater();  
  65.             View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast));  
  66.             ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);  
  67.             image.setImageResource(R.drawable.icon);  
  68.             TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);  
  69.             title.setText("Attention");  
  70.             TextView text = (TextView) layout.findViewById(R.id.tvTextToast);  
  71.             text.setText("完全自定义Toast");  
  72.             toast = new Toast(getApplicationContext());  
  73.             toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);  
  74.             toast.setDuration(Toast.LENGTH_LONG);  
  75.             toast.setView(layout);  
  76.             toast.show();  
  77.             break;  
  78.         case R.id.btnRunToastFromOtherThread:  
  79.             new Thread(new Runnable() {  
  80.                 public void run() {  
  81.                     showToast();  
  82.                 }  
  83.             }).start();  
  84.             break;  
  85.         }  
  86.     }  
  87. }  
main.xml代码
  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="5dip" >  
  8.   
  9.     <Button  
  10.         android:id="@+id/btnSimpleToast"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="默认" >  
  14.     </Button>  
  15.   
  16.     <Button  
  17.         android:id="@+id/btnSimpleToastWithCustomPosition"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="自定义显示位置" >  
  21.     </Button>  
  22.   
  23.     <Button  
  24.         android:id="@+id/btnSimpleToastWithImage"  
  25.         android:layout_width="fill_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:text="带图片" >  
  28.     </Button>  
  29.   
  30.     <Button  
  31.         android:id="@+id/btnCustomToast"  
  32.         android:layout_width="fill_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="完全自定义" >  
  35.     </Button>  
  36.   
  37.     <Button  
  38.         android:id="@+id/btnRunToastFromOtherThread"  
  39.         android:layout_width="fill_parent"  
  40.         android:layout_height="wrap_content"  
  41.         android:text="其他线程" >  
  42.     </Button>  
  43.   
  44. </LinearLayout>  
custom.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/llToast"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content"  
  6.     android:background="#ffffffff"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <TextView  
  10.         android:id="@+id/tvTitleToast"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_margin="1dip"  
  14.         android:background="#bb000000"  
  15.         android:gravity="center"  
  16.         android:textColor="#ffffffff" />  
  17.   
  18.     <LinearLayout  
  19.         android:id="@+id/llToastContent"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_marginBottom="1dip"  
  23.         android:layout_marginLeft="1dip"  
  24.         android:layout_marginRight="1dip"  
  25.         android:background="#44000000"  
  26.         android:orientation="vertical"  
  27.         android:padding="15dip" >  
  28.   
  29.         <ImageView  
  30.             android:id="@+id/tvImageToast"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_gravity="center" />  
  34.   
  35.         <TextView  
  36.             android:id="@+id/tvTextToast"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:gravity="center"  
  40.             android:paddingLeft="10dip"  
  41.             android:paddingRight="10dip"  
  42.             android:textColor="#ff000000" />  
  43.     </LinearLayout>  
  44.   
  45. </LinearLayout>  
用户评论