1. 程式人生 > >Toast工具類

Toast工具類

這是專案中常用到的Toast管理工具類,記錄於此。

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

/**
 * Created by LY on 2017/3/7.
 * 下一個Toast覆蓋上一個Toast
 */
public class ToastUtil {
    private static Toast mToast;
    private static Handler mHandler = new Handler();
    private static Runnable r = new Runnable() {
        public void run() {
            mToast.cancel();
        }
    };

    public static void makeText(Context mContext, String text) {

        mHandler.removeCallbacks(r);
        if (mToast != null)
            mToast.setText(text);
        else
            mToast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);
        mHandler.postDelayed(r, 2000);

        mToast.show();
    }

//    public static void makeText(Context mContext, int resId, int duration) {
//        makeText(mContext, mContext.getResources().getString(resId), duration);
//    }
}