Android Toast连续点击只弹一次

来源:互联网 发布:天金加银软件下载 编辑:程序博客网 时间:2024/05/01 15:35

在Android开发中,我们经常会使用到Toast,但是在使用的过程中发现了一个问题,当对一个事件进行连续操作弹出Toast时,Toast会连续的显示,这种体验可能不是很恰当,应该只提示一次就行了。下面对这个问题做了处理,解决了Toast多次弹出的情况。


public class ToastUtil  {

    private static Context context = null;
    private static Toast toast = null;


    public static void getShortToast(Context context,int retId){
        if (toast == null) {
            toast = Toast.makeText(context, retId, Toast.LENGTH_SHORT);
        } else {
            toast.setText(retId);
            toast.setDuration(Toast.LENGTH_SHORT);
        }


        toast.show();
    }


    public static void getShortToastByString(Context context,String hint){
        if (toast == null) {
            toast = Toast.makeText(context, hint, Toast.LENGTH_SHORT);
        } else {
            toast.setText(hint);
            toast.setDuration(Toast.LENGTH_SHORT);
        }


        toast.show();
    }


    public static void getLongToast(Context context,int retId){
        if (toast == null) {
            toast = Toast.makeText(context, retId, Toast.LENGTH_LONG);
        } else {
            toast.setText(retId);
            toast.setDuration(Toast.LENGTH_LONG);
        }


        toast.show();
    }


    public static void getLongToastByString(Context context,String hint){
        if (toast == null) {
            toast = Toast.makeText(context, hint, Toast.LENGTH_LONG);
        } else {
            toast.setText(hint);
            toast.setDuration(Toast.LENGTH_LONG);
        }


        toast.show();
    }
}
0 0
原创粉丝点击