How to solve the Android toast repeat display _android

Source: Internet
Author: User
Tags set time

Toast is a simple message balloon that does not get the focus, and will automatically disappear after the set time is displayed, typically for help or hints.

First of all to share my solution to the idea:

Instead of calculating the toast time, you define a global member variable toast, the toast is not NULL to make, Otherwise direct settext. In order to press the return key immediately after the toast no longer displayed, rewrite the parent activity onbackpressed () method inside to cancel your toast can.

Code:

Private Toast mtoast; 
public void Showtoast (String text) { 
if (mtoast = = null) { 
mtoast = Toast.maketext (testactivity.this, text, Toast. Length_short); 
} else { 
mtoast.settext (text); 
Mtoast.setduration (Toast.length_short); 
} 
Mtoast.show (); 
} 
public void Canceltoast () { 
if (mtoast!= null) { 
mtoast.cancel (); 
} 
} 
public void onbackpressed () { 
canceltoast (); 
Super.onbackpressed (); 

Here's how to fix the Android toast repeat display wait time too long

When the trigger Click event Display toast Information, if set the length of time is Length_long, although back to the background run, but still will display toast information, especially when consecutive clicks, toast will wait until all toast display, The user experience of this interface is very poor.

→1 Toast 2 Toast 3 Toast 4 Toast 5 toast→

In order to avoid this problem, you can toast the information display place to add a judgment, the method is as follows:

Private context Mcontext;
Private Toast mtoast;
if (mtoast!=null)
{
mtoast.settext (r.string.neterror); 
}
else
{
/* * First parameter: The current context environment, represented by this or getapplicationcontext ().
* Second parameter: The string displayed, expressed in r.string.
* Third parameter: The length of time displayed. expressed in Length_long (long) or length_short (short), or in milliseconds.
* *
mtoast=toast.maketext (mcontext,r.string.neterror, Toast.length_short);
} 
Mtoast.show (); Display toast Information

In addition, you can design your own style toast message box by setting other properties of toast.

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.