Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Toast 封裝:告別訊息覆蓋,打造最好用的提示元件

編輯:關於 Android 程式設計

系統原生的 Toast 有個惱人的問題:連續呼叫時訊息容易相互覆蓋、閃現不流暢。以下這份 ToastUtils 透過單例重用與主執行緒 Handler 的搭配,實現了訊息無覆蓋、無阻塞的效果,是替換系統 Toast 相當好用的封裝方案。

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

/**
 * Toast 工具類
 */
public class ToastUtils {
    private static Handler handler = new Handler(Looper.getMainLooper());
    private static Toast toast = null;
    private static Object synObj = new Object();

    /** 發送訊息,預設 Toast.LENGTH_SHORT */
    public static void showMessage(final Context act, final String msg) {
        showMessage(act, msg, Toast.LENGTH_SHORT);
    }

    /** 發送訊息,預設 Toast.LENGTH_LONG */
    public static void showMessageLong(final Context act, final String msg) {
        showMessage(act, msg, Toast.LENGTH_LONG);
    }

    /** 發送訊息,預設 Toast.LENGTH_SHORT */
    public static void showMessage(final Context act, final int msg) {
        showMessage(act, msg, Toast.LENGTH_SHORT);
    }

    /** 發送訊息,預設 Toast.LENGTH_LONG */
    public static void showMessageLong(final Context act, final int msg) {
        showMessage(act, msg, Toast.LENGTH_LONG);
    }

    /** 發送訊息 */
    public static void showMessage(final Context act, final int msg, final int len) {
        new Thread(new Runnable() {
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (synObj) {
                            if (toast != null) {
                                toast.cancel();
                                toast.setText(msg);
                                toast.setDuration(len);
                            } else {
                                toast = Toast.makeText(act, msg, len);
                            }
                            toast.show();
                        }
                    }
                });
            }
        }).start();
    }

    /** 發送訊息 */
    public static void showMessage(final Context act, final String msg, final int len) {
        new Thread(new Runnable() {
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (synObj) {
                            if (toast != null) {
                                toast.cancel();
                                toast.setText(msg);
                                toast.setDuration(len);
                            } else {
                                toast = Toast.makeText(act, msg, len);
                            }
                            toast.show();
                        }
                    }
                });
            }
        }).start();
    }

    /** 關閉當前 Toast */
    public static void cancelCurrentToast() {
        if (toast != null) {
            toast.cancel();
        }
    }
}

封裝的精髓在於:始終重用同一個 Toast 實例,並在顯示前先 cancel 掉舊訊息,再切回主執行緒更新內容,如此一來便能徹底避免訊息堆疊覆蓋,帶來順暢的提示體驗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved