美文网首页
安卓全局toast

安卓全局toast

作者: guanyi | 来源:发表于2017-09-05 10:50 被阅读0次

使用全局toast,防止toast因为消息过多,弹个不停

public class CustomToast {
    private static int mDuration = 5000;//toast默认的时间

    private static Toast mToast;
    private static Handler mHandler = new Handler();
    private static Runnable r = new Runnable() {
        public void run() {
            mToast.cancel();
        }
    };
 
    public static void showToast(Context mContext, String text) {
        showToast(mContext, text, mDuration);
    }

    public static void showToast(Context mContext, int resId) {
        showToast(mContext, mContext.getResources().getString(resId), mDuration);
    }

    public static void showToast(Context mContext, String text, int duration) {

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

        mToast.show();

    }

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

}


相关文章

网友评论

      本文标题:安卓全局toast

      本文链接:https://www.haomeiwen.com/subject/uqchdxtx.html