Android中用Toast.cancel()方法优化toast内容的显示

产品在测试过程中发现一个bug,就是测试人员不停的疯狂的点击某个按钮,触发了toast以后,toast内容会一直排着队的显示出来,不能很快的消失。这样可能会影响用户的使用。

看到Toast有一个cancel()方法:

 

void cancel()
Close the view if it's showing, or don't show it if it isn't showing yet.

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

 

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

 

  1. package com.arui.framework.android.util;  
  2.   
  3. import android.content.Context;  
  4. import android.os.Handler;  
  5. import android.os.Looper;  
  6. import android.widget.Toast;  
  1. /**    
  2.  * Toast util class.    
  3.  *     
  4.  * @author <a href="http://blog.csdn.net/arui319">http://blog.csdn.net/arui319</a>    
  5.  * @version 2011/11/30    
  6.  *     
  7.  */   
  8. public class ToastUtil {  
  9.   
  10.     private static Handler handler = new Handler(Looper.getMainLooper());  
  11.   
  12.     private static Toast toast = null;  
  13.       
  14.     private static Object synObj = new Object();  
  15.   
  16.     public static void showMessage(final Context act, final String msg) {  
  17.         showMessage(act, msg, Toast.LENGTH_SHORT);  
  18.     }  
  19.   
  20.     public static void showMessage(final Context act, final int msg) {  
  21.         showMessage(act, msg, Toast.LENGTH_SHORT);  
  22.     }  
  23.   
  24.     public static void showMessage(final Context act, final String msg,  
  25.             final int len) {  
  26.         new Thread(new Runnable() {  
  27.             public void run() {  
  28.                 handler.post(new Runnable() {  
  29.                     @Override  
  30.                     public void run() {  
  31.                         synchronized (synObj) {  
  32.                             if (toast != null) {  
  33.                                 toast.cancel();  
  34.                                 toast.setText(msg);  
  35.                                 toast.setDuration(len);  
  36.                             } else {  
  37.                                 toast = Toast.makeText(act, msg, len);  
  38.                             }  
  39.                             toast.show();  
  40.                         }  
  41.                     }  
  42.                 });  
  43.             }  
  44.         }).start();  
  45.     }  
  46.   
  47.   
  48.     public static void showMessage(final Context act, final int msg,  
  49.             final int len) {  
  50.         new Thread(new Runnable() {  
  51.             public void run() {  
  52.                 handler.post(new Runnable() {  
  53.                     @Override  
  54.                     public void run() {  
  55.                         synchronized (synObj) {  
  56.                             if (toast != null) {  
  57.                                 toast.cancel();  
  58.                                 toast.setText(msg);  
  59.                                 toast.setDuration(len);  
  60.                             } else {  
  61.                                 toast = Toast.makeText(act, msg, len);  
  62.                             }  
  63.                             toast.show();  
  64.                         }  
  65.                     }  
  66.                 });  
  67.             }  
  68.         }).start();  
  69.     }  
  70.   
  71. }  


 


代码的逻辑很简单。这里加了同步,这样做可以确保每一个toast的内容至少可以显示出来,而不是还没显示就取消掉了。这样做,是因为toast的内容不一定完全相同,如果没显示出来,也会有问题。

猜你喜欢

转载自chenshichao.iteye.com/blog/2068110