问题
I try to set my Toast show duration like 1minute. I try this:
final Toast toast = Toast.makeText(getApplicationContext(), "MESSAGE", Toast.LENGTH_LONG );
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 60000);
Thanks for your help.
回答1:
Since LENGTH_SHORT is 2 seconds (and LENGTH_LONG is 3.5 seconds), try this:
for (int i=0; i < 30; i++)
{
Toast.makeText(this, "MESSAGE", Toast.LENGTH_SHORT).show();
}
回答2:
There are only two possible Toast
durations: short (2 sec) and long (3.5 sec).
If you need a more persistent message, use a dialog or include the message in your layout.
One easy way to make context-sensitive messages in your layout with custom durations is the Crouton library.
回答3:
Take a look at this answer.
The values of
LENGTH_SHORT
andLENGTH_LONG
are 0 and 1. This means they are treated as flags rather than actual durations so I don't think it will be possible to set the duration to anything other than these values.
回答4:
final Toast tag = Toast.makeText(getBaseContext(), "YOUR MESSAGE",Toast.LENGTH_SHORT);
tag.show();
new CountDownTimer(9000, 1000) {
public void onTick(long millisUntilFinished) {tag.show();}
public void onFinish() {tag.show();}
}.start();
See Similar Question and answer there.
回答5:
Toasts are not meant to be used like that. Toasts are transient and Android has defined them to be either SHORT or LONG.
If you want, you can create a Dialog than completely emulated the appearance of a Toast, but I would use a dismissable dialog or a notification as it could be frustrating for the user to have a Toast showing for a whole minute without the possibility to dismiss it.
回答6:
if you want to show toast by your choice you have to make a custom toast with time duartion that you want
View toastview = findViewById(R.id.customtoastlayout);
LinearLayout mToastLayout =
toastview.findViewById(R.id.toastlayout);
TextView text = toastview.findViewById(R.id.customToastText);
text.setText(message);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.TOP | Gravity.RIGHT, 90, 0);
toast.setDuration(duration);//custom duartion
toast.setView(mToastLayout);
toast.show();
来源:https://stackoverflow.com/questions/21134640/set-android-toast-duration-to-be-really-long-e-g-1-minute