没处理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, "内容");
本文介绍了一种优化Toast显示的方法,通过创建一个Toast工具类避免了重复点击时Toast频繁弹出的问题,提高了用户体验。





