Android Toast 重复显示问题


做程序员的,基本一看api就知道,用这个可以取消上一个toast的显示,然后显示下一个,这样就能解决出现的问题。可是在测试的过程中,发现却没有想象中的那么简单,不信可以百度一下,很多很多人发现toast的cancel()方法不起作用。还是不讲具体过程,只讲结果吧。

我把toast做成了一个应用类,方便使用,大家可以直接用:


[java]  view plain copy
  1. public class ToastUtil {    
  2.     
  3.     private static Handler handler = new Handler(Looper.getMainLooper());    
  4.     
  5.     private static Toast toast = null;    
  6.         
  7.     private static Object synObj = new Object();    
  8.     
  9.     public static void showMessage(final Context act, final String msg) {    
  10.         showMessage(act, msg, Toast.LENGTH_SHORT);    
  11.     }    
  12.     
  13.     public static void showMessage(final Context act, final int msg) {    
  14.         showMessage(act, msg, Toast.LENGTH_SHORT);    
  15.     }    
  16.     
  17.     public static void showMessage(final Context act, final String msg,    
  18.             final int len) {    
  19.         new Thread(new Runnable() {    
  20.             public void run() {    
  21.                 handler.post(new Runnable() {    
  22.                     @Override    
  23.                     public void run() {    
  24.                         synchronized (synObj) {    
  25.                             if (toast != null) {    
  26.                                 toast.cancel();    
  27.                                 toast.setText(msg);    
  28.                                 toast.setDuration(len);    
  29.                             } else {    
  30.                                 toast = Toast.makeText(act, msg, len);    
  31.                             }    
  32.                             toast.show();    
  33.                         }    
  34.                     }    
  35.                 });    
  36.             }    
  37.         }).start();    
  38.     }    
  39.     
  40.     
  41.     public static void showMessage(final Context act, final int msg,    
  42.             final int len) {    
  43.         new Thread(new Runnable() {    
  44.             public void run() {    
  45.                 handler.post(new Runnable() {    
  46.                     @Override    
  47.                     public void run() {    
  48.                         synchronized (synObj) {    
  49.                             if (toast != null) {    
  50.                                 toast.cancel();    
  51.                                 toast.setText(msg);    
  52.                                 toast.setDuration(len);    
  53.                             } else {    
  54.                                 toast = Toast.makeText(act, msg, len);    
  55.                             }    
  56.                             toast.show();    
  57.                         }    
  58.                     }    
  59.                 });    
  60.             }    
  61.         }).start();    
  62.     }    
  63.     
  64. }    


原文地址:http://blog.csdn.net/tounaobun/article/details/8274276

代码的逻辑很简单。这里加了同步,这样做可以确保每一个toast的内容至少可以显示出来,而不是还没显示就取消掉了。这样做,是因为toast的内容不一定完全相同,如果没显示出来,也会有问题。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值