Android 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(); 
        
    

Android Toast 封装,避免Toast消息覆盖,替换系统Toast最好用的封装的更多相关文章

  1. 【Android代码片段之六】Toast工具类(实现带图片的Toast消息提示)

    转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/6841266  作者:张燕广 实现的Toast工具类ToastUtil封装 ...

  2. Android:谈一谈安卓应用中的Toast情节(基础)

    前言 Toast,这个曾经也是现在正在迷倒万千软件开发者尤其是android开发者的小美女,向来不乏在各个明星应用中频繁登场.Toast是神马~听说是一种吐司面包,能吃吗?如果手机屏幕是巧克力做的,我 ...

  3. 第三部分:Android 应用程序接口指南---第二节:UI---第八章 Toast通知

    第8章 Toast通知 Toast通知是在窗口前面弹出的信息.它只占有信息所需要的空间量,并且用户当前的activity仍然是可见的.可互动的.这种通知自动地淡入和淡出,它不接受交互事件.他相当于一种 ...

  4. Android_(消息提示)多种使用Toast的消息提示

    Toast是一种提供给用户简介信息的视图,可以创建和显示消息,该视图以浮于应用程序之上的形式呈现给用户.因为它并不获得焦点,即使用户正在输入什么也不会受到影响. Toast目标是尽可能以不显眼的方式, ...

  5. Android开发之接收系统广播消息

    BroadcastReceiver除了接收用户所发送的广播消息之外.另一个重要的用途:接收系统广播. 假设应用须要在系统特定时刻运行某些操作,就能够通过监听系统广播来实现.Android的大量系统事件 ...

  6. Android——AutoCompleteTextView、Spinner和消息提示

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  7. Android基础------高级ul:消息提示

    前言:Android消息提示笔记,刚刚接触Android 1.静态方法Toast 直接调用静态方法 //消息提示(context,"内容",固定时间) Toast.makeText ...

  8. Android中利用Handler实现消息的分发机制(三)

    在第二篇文章<Android中利用Handler实现消息的分发机制(一)>中,我们讲到主线程的Looper是Android系统在启动App的时候,已经帮我们创建好了,而假设在子线程中须要去 ...

  9. Android学习系列(7)--App消息通知机制

    有人说,程序员很安静,但我不完全同意,程序员的聒噪,是藏在代码后面,是藏在程序后面.这篇文章是Android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用. 1.消息推送机制    ...

随机推荐

  1. Python3常用内置函数

    数学相关 abs(a) : 求取绝对值.abs(-1) max(list) : 求取list最大值.max([1,2,3]) min(list) : 求取list最小值.min([1,2,3]) su ...

  2. Orchard常见问题

    本文链接:http://www.cnblogs.com/souther/p/4543299.html 什么是Orchard Orchard是一个免费,开源,注重社区的项目,其目标是提供ASP.NET平 ...

  3. post数据

    http://cnodejs.org/topic/4f16442ccae1f4aa2700104d http://www.cnblogs.com/pingfan1990/p/4701355.html ...

  4. 百度地图 api 功能封装类 (ZMap.js) 新增管理事件功能 [源码下载]

    ZMap 功能说明 ZMap.js 本类方法功能大多使用 prototype 原型 实现: 包含的功能有:轨迹回放,圈画区域可编辑,判断几个坐标是否在一个圆圈内,生活服务查询,从经纬度获取地址信息,地 ...

  5. HTML5——多次定位请求

    多次定位请求及点击一次 就不断的触发请求事件,和单次定位请求写法差不多,只需要将 navigator.geolocation.getCurrentPosition改为navigator.geoloca ...

  6. Ibatis中传List参数

    Ibatis中用list传参数的方式. Java代码  select count(id) from `user` where id in #[]# and status=1 . <select ...

  7. 【Gym 100947E】Qwerty78 Trip(组合数取模/费马小定理)

    从(1,1)到(n,m),每次向右或向下走一步,,不能经过(x,y),求走的方案数取模.可以经过(x,y)则相当于m+n步里面选n步必须向下走,方案数为 C((m−1)+(n−1),n−1) 再考虑其 ...

  8. 【ZOJ 1221】Risk

    题 题意 给你20个城市的相邻关系,求给定任意两个城市的最短距离 分析 求任意两个城市最短距离,就是用floyd算法,我脑残忘记了k是写在最外层的. 代码 #include<stdio.h> ...

  9. 【HDU 1757】 A Simple Math Problem

    题 Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 1 ...

  10. 17.(转) Android之四大基本组件介绍与生命周期

    Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器. 一:了解四大基本组件 Activity ...