Android Toast 简单封装

来源:互联网 发布:手机淘宝上买东西没钱 编辑:程序博客网 时间:2024/05/16 08:02

Android Toast 简单封装且解决了Toast显示时间叠加问题。

ToastUtil代码:

public class ToastUtil {    private static Toast mToast = null;    public static void showToast(Context context, String text, int duration) {        cancelToast();        mToast = Toast.makeText(context, text, duration);        mToast.show();    }    public static void showToastShort(Context context, String text) {        showToast(context, text, Toast.LENGTH_SHORT);    }    public static void showToastShort(Context context, int resId) {        showToastShort(context, context.getString(resId));    }    public static void showToastLong(Context context, String text) {        showToast(context, text, Toast.LENGTH_LONG);    }    public static void showToastLong(Context context, int resId) {        showToastLong(context, context.getString(resId));    }    public static void cancelToast() {        if (mToast != null) {            mToast.cancel();        }    }}

使用:

ToastUtil.showToastShort(this,R.string.app_name);




0 0
原创粉丝点击