當我們快速連續點擊了五次Toast按鈕,Toast就觸發了五次。這樣的體驗其實是不好的,因爲也許用戶是手抖了一下多點了幾次,導致Toast就長時間關閉不掉了。又或者我們其實已在進行其他操作了,應該彈出新的Toast提示,而上一個Toast卻還沒顯示結束。
因此,最佳的做法是將Toast的調用封裝成一個接口,寫在一個公共的類當中,如下所示:
public class Util {
private static Toast toast;
public static void showToast(Context context,
String content) {
if (toast == null) {
toast = Toast.makeText(context,
content,
Toast.LENGTH_SHORT);
} else {
toast.setText(content);
}
toast.show();
}
}