当我们在一个应用中用到Toaster来做为提示时,发现这样一个问题,当某个条件服合时,会弹出Toaster的对话框,不停地执行这个条件,会不停进行Toaster.show的显示,执行几次就现示几次,即使这个应用程序退出也会不停地Toast.show地显示,这样一来会给用户带来一种不好体验。当我们将应用程序退出了,就不应该Toast.show显示了。
我们可以在应用程序退出onDestroy()时,进行Toaster.cancel().就可以实现了,但Toaster必须时全局的,同一个Toaster
mToast = new Toast(this); //OnCreate ()
LayoutInflater inflater = LayoutInflater.from(this);
View view2 = inflater.inflate(R.layout.toas, null);
mToast.setView(view2);
mToast.setDuration(1000);
创建一个LayoutInflater,在LayoutInflater中有:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:background="#ff000000"
android:layout_height="wrap_content" android:text="@string/most_char" />
</LinearLayout>
按上述即可以实现对Toaster.show的控制。