自定义Toast、程序退出时Toast也退出、Toast的用法

时间:2023-04-20 23:18:27
自定义Toast、程序退出时Toast也退出、Toast的用法

http://blog.csdn.net/wangqilin8888/article/details/7464806

当我们在一个应用中用到Toaster来做为提示时,发现这样一个问题,当某个条件服合时,会弹出Toaster的对话框,不停地执行这个条件,会不停进行Toaster.show的显示,执行几次就现示几次,即使这个应用程序退出也会不停地Toast.show地显示,这样一来会给用户带来一种不好体验。当我们将应用程序退出了,就不应该Toast.show显示了。

我们可以在应用程序退出onDestroy()时,进行Toaster.cancel().就可以实现了,但Toaster必须时全局的,同一个Toaster

  1. mToast = new Toast(this);    //OnCreate ()
  2. LayoutInflater inflater = LayoutInflater.from(this);
  3. View view2 = inflater.inflate(R.layout.toas, null);
  4. mToast.setView(view2);
  5. mToast.setDuration(1000);
  6. 创建一个LayoutInflater,在LayoutInflater中有:
  7. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  8. android:orientation="vertical" android:layout_width="wrap_content"
  9. android:layout_height="wrap_content">
  10. <TextView android:layout_width="wrap_content"
  11. android:background="#ff000000"
  12. android:layout_height="wrap_content" android:text="@string/most_char" />
  13. </LinearLayout>

按上述即可以实现对Toaster.show的控制。