Toast工具類
阿新 • • 發佈:2018-11-26
這是專案中常用到的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); // } }