Set Android Toast duration to be really long (e.g., 1 minute)

a 夏天 提交于 2020-02-03 08:28:06

问题


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 and LENGTH_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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!