Home>

toast is a simple message prompt box,It cannot get focus,It will disappear automatically after the set time.Generally used for help or tips,This article introduces the solution of android toast repeated display,Related to android toast related knowledge

toast is a simple message prompt box,It cannot get focus,It will disappear automatically after the set time.Generally used for help or tips.

Let me share with you my solution:

No need to calculate the time of toast, etc.Is to define a global member variable toast, go to make when this toast is not null, otherwise settext directly. In order to make toast no longer displayed immediately after pressing the return key,Override the onbackpressed () method of the parent activity to cancel your toast.

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

The following introduces the problem of resolving the long wait time of android toast.

When the click event is triggered to display toast information, if the length type is set to length_long, although it returns to the background,But the toast message is still displayed, especially when clicking continuously,toast will wait in line until all toasts are displayed,The user experience of this interface is very poor.

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

To avoid this problem,You can add a judgment to where the toast information is displayed,Methods as below:

private context mcontext;
private toast mtoast;
if (mtoast!=null)
{
mtoast.settext (r.string.neterror);
}
else
{
/*
* The first parameter:the current context,Use this or getapplicationcontext ().
* The second parameter:the displayed string,Represented by r.string.
* The third parameter:the length of time displayed.
It is expressed by length_long (length) or length_short (short), or milliseconds.
* /
mtoast=toast.maketext (mcontext, r.string.neterror, toast.length_short);
}
mtoast.show ();//Show toast information

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

  • Previous Analysis of List () Function Usage in PHP
  • Next Tutorial for installing MongoDB and its start command mongod in Ubuntu