문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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.

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.

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.

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.

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top