Android uses the Toast.cancel () method to optimize Toast content display Solutions _android

Source: Internet
Author: User

Products in the test process found a bug, is the tester constantly crazy click on a button, triggered the toast, toast content will always line up the display, not quickly disappear. This may affect the user's use.

See Toast has a Cancel () method:

Copy Code code as follows:

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

As a programmer, the basic view of the API is that you can use this to cancel the last toast display, and then show the next, so you can solve the problem. But in the process of testing, found that there is no imagination so simple, not letter can Baidu, many many people found toast Cancel () method does not work. Or do not talk about the specific process, just talk about the results.

I made the toast into an application class, easy to use, you can directly use:

Copy Code code as follows:

Package com.arui.framework.android.util;

Import Android.content.Context;
Import Android.os.Handler;
Import Android.os.Looper;
Import Android.widget.Toast;

Copy Code code as follows:

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

}

The logic of the code is simple. This is synchronized, which ensures that the content of each toast can be displayed at least, rather than canceled without being shown. This is done because the contents of the toast are not necessarily identical, and if they are not shown, there will be problems.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.