Toast类避免显示时间叠加的方法
作者:琦琦安卓进阶 时间:2021-08-20 02:01:42
本文为大家分享了Toast类避免显示时间叠加的方法,供大家参考,具体内容如下
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.widget.Toast;
/**
* Toast工具类
* Created by user on 2016/12/22.
*/
public class ToastUtil {
private static Toast toast = null;
private static ToastUtil toastUtil = null;
public ToastUtil(){}
public synchronized static ToastUtil getInstance(){
if(null == toastUtil){
toastUtil = new ToastUtil();
}
return toastUtil;
}
public void showToast(Context context, String string){
if(toast != null){
toast.cancel();
}
toast = Toast.makeText(context, string,Toast.LENGTH_SHORT);
toast.show();
}
public void showToast(Fragment fragment, String string){
showToast(fragment.getActivity(),string);
}
public void showToast(Activity activity, String string){
if(toast != null){
toast.cancel();
}
toast = Toast.makeText(activity, string,Toast.LENGTH_SHORT);
toast.show();
}
public void showToastTest(Context context){
if(toast != null){
toast.cancel();
}
toast = Toast.makeText(context, "click",Toast.LENGTH_SHORT);
toast.show();
}
public void showToastTest(Fragment fragment){
showToastTest(fragment.getActivity());
}
public void showToastTest(Activity activity){
if(toast != null){
toast.cancel();
}
toast = Toast.makeText(activity, "click",Toast.LENGTH_SHORT);
toast.show();
}
}
if(null == toastUtil){
toastUtil = new ToastUtil();
}
return toastUtil;
}
public void showToastInThread(Context context,String msg){
Looper.prepare();
if(toast != null){
toast.cancel();
}
toast = Toast.makeText(context,msg,Toast.LENGTH_SHORT);
toast.show();
Looper.loop();
}
public void showToast(Context context, String string){
if(toast != null){
toast.cancel();
}
toast = Toast.makeText(context, string,Toast.LENGTH_SHORT);
toast.show();
}
}
这是一个封装好的Toast工具类,避免时间叠加
使用方法
ToastUtil.getInstance().showToast(mContext,"test");
如果在线程中执行的话,必须按照如下格式
ToastUtil.getInstance().showToastInThread(mContext,"str");
来源:https://blog.csdn.net/qq_28695619/article/details/53813177
标签:Toast,显示时间,时间叠加
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android UI效果之绘图篇(四)
2022-08-07 19:26:12
![](https://img.aspxhome.com/file/2023/2/126692_0s.png)
Java中常用的设计模式之观察者模式详解
2023-07-27 12:32:52
![](https://img.aspxhome.com/file/2023/0/75450_0s.png)
java实现哈弗曼编码与反编码实例分享(哈弗曼算法)
2023-11-25 04:54:05
如何利用反射构建元数据查看器
2021-05-27 00:11:05
java 引用传递的三种类型小结
2023-09-03 03:59:47
![](https://img.aspxhome.com/file/2023/0/60560_0s.png)
C# 异步多线程入门到精通之ThreadPool篇
2022-03-23 01:51:00
![](https://img.aspxhome.com/file/2023/0/102200_0s.png)
Mybatis order by 动态传参出现的问题及解决方法
2022-07-26 04:13:09
![](https://img.aspxhome.com/file/2023/3/64373_0s.png)
Android WindowManger的层级分析详解
2023-08-05 23:51:40
教你怎么用Java开发扫雷游戏
2023-07-22 09:49:26
![](https://img.aspxhome.com/file/2023/0/63480_0s.png)
Java中DataInputStream和DataOutputStream的使用方法
2023-02-03 04:08:28
![](https://img.aspxhome.com/file/2023/9/110909_0s.webp)
SpringBoot整合Elasticsearch并实现CRUD操作
2021-10-28 07:27:31
![](https://img.aspxhome.com/file/2023/2/69282_0s.png)
新手小白学JAVA IDEA下载使用手册全集
2023-11-23 14:33:23
![](https://img.aspxhome.com/file/2023/3/59773_0s.jpg)
解析在内部循环中Continue外部循环的使用详解
2023-09-24 08:03:50
SpringMVC深入讲解文件的上传下载实现
2022-04-21 09:01:51
![](https://img.aspxhome.com/file/2023/8/71648_0s.png)
Java中关于Map四种取值方式
2022-04-22 09:24:09
IntelliJ IDEA 2020.2正式发布,两点多多总能助你提效
2023-08-30 18:15:18
![](https://img.aspxhome.com/file/2023/2/58082_0s.jpg)
Java面向对象实现汽车租赁系统
2023-05-20 07:03:06
![](https://img.aspxhome.com/file/2023/8/64428_0s.jpg)
springboot集成redis并使用redis生成全局唯一索引ID
2023-11-28 05:42:39
![](https://img.aspxhome.com/file/2023/0/59020_0s.jpg)
struts2框架的登录制作图文教程
2022-11-24 03:15:15
![](https://img.aspxhome.com/file/2023/1/70981_0s.png)
C语言实现贪吃蛇小游戏开发
2022-08-20 00:03:07
![](https://img.aspxhome.com/file/2023/7/120827_0s.jpg)