当前位置: 移动技术网 > 移动技术>移动开发>Android > Android中使用Toast.cancel()方法优化toast内容显示的解决方法

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

产品在测试过程中发现一个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的内容不一定完全相同,如果没显示出来,也会有问题。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网