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

时间:2021-12-14 05:17:03 

产品在测试过程中发现一个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做成了一个应用类,方便使用,大家可以直接用:


package com.arui.framework.android.util; 

import android.content.Context; 
import android.os.Handler; 
import android.os.Looper; 
import android.widget.Toast; 



/**   
 * Toast util class.   
 *    
 * @author <A href="http://jb51.net">http://jb51.net</A>   
 * @version 2011/11/30   
 *    
 */  
public class ToastUtil { 

    private static Handler handler = new Handler(Looper.getMainLooper()); 

    private static Toast toast = null; 

    private static Object synObj = new Object(); 

    public static void showMessage(final Context act, final String msg) { 
        showMessage(act, msg, Toast.LENGTH_SHORT); 
    } 

    public static void showMessage(final Context act, final int msg) { 
        showMessage(act, msg, Toast.LENGTH_SHORT); 
    } 

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

 
    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的内容至少可以显示出来,而不是还没显示就取消掉了。这样做,是因为toast的内容不一定完全相同,如果没显示出来,也会有问题。

标签:android,cancel,toast
0
投稿

猜你喜欢

  • Java中对list map根据map某个key值进行排序的方法

    2023-09-04 17:10:03
  • Spring Boot CLI使用教程

    2023-03-30 03:37:02
  • Spring Cloud中Sentinel的两种限流模式介绍

    2021-11-22 00:38:10
  • C#窗体传值代码方法

    2022-12-03 22:57:37
  • 如何使用ByteArrayOutputStream下载文件

    2021-07-29 11:32:45
  • Java利用Selenium操作浏览器的示例详解

    2022-06-17 17:34:20
  • Android Flutter中Offstage组件的使用教程详解

    2023-08-20 01:01:44
  • 关于mybatis使用${}时sql注入的问题

    2023-04-18 03:29:40
  • spring boot 动态生成接口实现类的场景分析

    2022-12-14 05:09:33
  • springBoot整合rabbitMQ的方法详解

    2022-08-19 02:28:33
  • Java实现插入排序算法可视化的示例代码

    2021-08-06 19:35:50
  • IDEA设置背景为自定义照片的操作方法

    2022-12-28 09:13:08
  • sqlite数据库的介绍与java操作sqlite的实例讲解

    2023-05-09 03:07:40
  • Java异步调用转同步方法实例详解

    2023-11-01 20:38:25
  • 解析C#多线程编程中异步多线程的实现及线程池的使用

    2021-10-23 02:36:23
  • java中TESTful架构原理分析

    2022-03-02 21:12:10
  • Java的SPI机制实例详解

    2021-08-15 08:47:41
  • c# BackgroundWorker使用方法

    2021-05-27 00:49:12
  • 详解基于MVC的数据查询模块进行模糊查询

    2022-02-13 19:26:49
  • Java Email邮件发送简单实现介绍

    2023-10-07 01:05:11
  • asp之家 软件编程 m.aspxhome.com