博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Toast 封装,避免Toast消息覆盖,替换系统Toast最好用的封装
阅读量:5106 次
发布时间:2019-06-13

本文共 3286 字,大约阅读时间需要 10 分钟。

 Toast 封装,避免Toast消息覆盖,无阻塞,等强大功能

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import
android.content.Context; 
import
android.os.Handler; 
import
android.os.Looper; 
import
android.widget.Toast; 
   
/**
 
* Toast工具类
 
* @author WikerYong   Email:<a href="#">yw_312@foxmail.com</a>
 
* @version 2012-5-21 下午9:21:01
 
*/ 
public
class
ToastUtils { 
    
private
static
Handler handler =
new
Handler(Looper.getMainLooper()); 
    
private
static
Toast toast =
null
    
private
static
Object synObj =
new
Object(); 
   
    
/**
     
* Toast发送消息,默认Toast.LENGTH_SHORT
     
* @author WikerYong   Email:<a href="#">yw_312@foxmail.com</a>
     
* @version 2012-5-22 上午11:13:10
     
* @param act
     
* @param msg
     
*/ 
    
public
static
void
showMessage(
final
Context act,
final
String msg) { 
        
showMessage(act, msg, Toast.LENGTH_SHORT); 
    
       
    
/**
     
* Toast发送消息,默认Toast.LENGTH_LONG
     
* @author WikerYong   Email:<a href="#">yw_312@foxmail.com</a>
     
* @version 2012-5-22 上午11:13:10
     
* @param act
     
* @param msg
     
*/ 
    
public
static
void
showMessageLong(
final
Context act,
final
String msg) { 
        
showMessage(act, msg, Toast.LENGTH_LONG); 
    
   
    
/**
     
* Toast发送消息,默认Toast.LENGTH_SHORT
     
* @author WikerYong   Email:<a href="#">yw_312@foxmail.com</a>
     
* @version 2012-5-22 上午11:13:35
     
* @param act
     
* @param msg
     
*/ 
    
public
static
void
showMessage(
final
Context act,
final
int
msg) { 
        
showMessage(act, msg, Toast.LENGTH_SHORT); 
    
       
    
/**
     
* Toast发送消息,默认Toast.LENGTH_LONG
     
* @author WikerYong   Email:<a href="#">yw_312@foxmail.com</a>
     
* @version 2012-5-22 上午11:13:35
     
* @param act
     
* @param msg
     
*/ 
    
public
static
void
showMessageLong(
final
Context act,
final
int
msg) { 
        
showMessage(act, msg, Toast.LENGTH_LONG); 
    
   
    
/**
     
* Toast发送消息
     
* @author WikerYong   Email:<a href="#">yw_312@foxmail.com</a>
     
* @version 2012-5-22 上午11:14:09
     
* @param act
     
* @param msg
     
* @param len
     
*/ 
    
public
static
void
showMessage(
final
Context act,
final
int
msg, 
            
final
int
len) { 
        
new
Thread(
new
Runnable() { 
            
public
void
run() { 
                
handler.post(
new
Runnable() { 
   
                    
@Override 
                    
public
void
run() { 
                        
synchronized
(synObj) { 
                            
if
(toast !=
null
) { 
                                
toast.cancel(); 
                                
toast.setText(msg); 
                                
toast.setDuration(len); 
                            
}
else
                                
toast = Toast.makeText(act, msg, len); 
                            
                            
toast.show(); 
                        
                    
                
}); 
            
        
}).start(); 
    
       
    
/**
     
* Toast发送消息
     
* @author WikerYong   Email:<a href="#">yw_312@foxmail.com</a>
     
* @version 2012-5-22 上午11:14:27
     
* @param act
     
* @param msg
     
* @param len
     
*/ 
    
public
static
void
showMessage(
final
Context act,
final
String msg, 
            
final
int
len) { 
        
new
Thread(
new
Runnable() { 
            
public
void
run() { 
                
handler.post(
new
Runnable() { 
   
                    
@Override 
                    
public
void
run() { 
                        
synchronized
(synObj) { 
                            
if
(toast !=
null
) { 
                                
toast.cancel(); 
                                
toast.setText(msg); 
                                
toast.setDuration(len); 
                            
}
else
                                
toast = Toast.makeText(act, msg, len); 
                            
                            
toast.show(); 
                        
                    
                
}); 
            
        
}).start(); 
    
   
    
/**
     
* 关闭当前Toast
     
* @author WikerYong   Email:<a href="#">yw_312@foxmail.com</a>
     
* @version 2012-5-22 上午11:14:45
     
*/ 
    
public
static
void
cancelCurrentToast() { 
        
if
(toast !=
null
) { 
            
toast.cancel(); 
        
    

转载于:https://www.cnblogs.com/hudabing/p/3741487.html

你可能感兴趣的文章
如何在maven工程中加载oracle驱动
查看>>
Flask 系列之 SQLAlchemy
查看>>
iframe跨域与session失效问题
查看>>
aboutMe
查看>>
【Debug】IAR在线调试时报错,Warning: Stack pointer is setup to incorrect alignmentStack,芯片使用STM32F103ZET6...
查看>>
一句话说清分布式锁,进程锁,线程锁
查看>>
Hash和Bloom Filter
查看>>
SQL Server获取月度列表
查看>>
python常用函数
查看>>
python 描点画圆
查看>>
FastDFS使用
查看>>
服务器解析请求的基本原理
查看>>
pycharm 如何设置方法调用字体颜色
查看>>
VUE源码解析心得
查看>>
[HDU3683 Gomoku]
查看>>
【工具相关】iOS-Reveal的使用
查看>>
整体二分——[Poi2011]Meteors
查看>>
数据库3
查看>>
delphi之事件
查看>>
windows server 2008 r2 安装
查看>>