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.