Android完美解决多次点击Toast一直提示不消失问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/codekxx/article/details/70851532

没处理Toast前,点多少次就提示多少次,体验很不友好

这里写图片描述

代码:

Toast.makeText(context,"内容",Toast.LENTH_SHORT).show

处理Toast后效果:

这里写图片描述

代码:

/**
 * Toast工具类
 */
public class ToastUtil {  

    private static Toast toast;  

    public static void showToast(Context context,   
        String content) {  
        if (toast == null) {  
            toast = Toast.makeText(context,  
                         content,   
                         Toast.LENGTH_SHORT);  
        } else {  
            toast.setText(content);  
        }  
        toast.show();  
    }  

}  

调用的时候也很简单,只需要把Context对象和Toast要显示的内容传进来就可以了:

ToastUtil.showToast(context, "内容");  

猜你喜欢

转载自blog.csdn.net/codekxx/article/details/70851532