如何设置Toast显示时间小于Toast.LENGTH_SHORT

问题描述 投票:23回答:8

我想要显示比Toast.LENGTH_SHORT更少的吐司,因为我觉得它需要大约2秒钟。我想要显示吐司只有半秒钟。

什么是Toast.LENGTH_SHORT和Toast.LENGTH_LONG的时间间隔?

android toast
8个回答
23
投票

只有两个可能的值:

private static final int LONG_DELAY = 3500; // 3.5 seconds
private static final int SHORT_DELAY = 2000; // 2 seconds

设置其他值不起作用。如果持续时间不等于1(Toast.LENGTH_LONG),那么持续时间将为SHORT_DELAY(2秒):

long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);

Toast的来源写道

这一次可以由用户定义。

但我找不到办法做到这一点。

更新:这里有解决方案:Set Toast Appear Length


28
投票

这对我有用

final Toast toast = Toast.makeText(getApplicationContext(), "The following message will disappear in half second", Toast.LENGTH_SHORT);
    toast.show();

    Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
               toast.cancel(); 
           }
    }, 500);

10
投票

请参阅我建议的解决方案here。您基本上在比标准Toast持续时间短的指定延迟之后调用toast.cancel()。


2
投票

试试这个

final Toast toast = Toast.makeText(getBaseContext(), "YOUR MESSAGE",Toast.LENGTH_SHORT);
            toast.show();
            new CountDownTimer(10000, 1000)
            {
                public void onTick(long millisUntilFinished) {toast.show();}
                public void onFinish() {toast.cancel();}
            }.start();

希望这有帮助..享受.. !!!


1
投票

对于新手,我做了最简单的解决方案 - 一种方法。

public void showToastMessage(String text, int duration){
        final Toast toast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
        toast.show();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, duration);
}

你还应该导入:

import android.os.Handler;
import android.widget.Toast;

您可以调用此方法,例如:

showToastMessage("your noob", 1000);

上面的方法只能在Fragment中使用!如果您希望它在Activity中工作,请在toast消息中将getActivity()替换为getApplicationContext()。祝你好运!


0
投票

它会工作。


   public void toastMessage(final String message) {
    this.runOnUiThread(new Runnable() {
        public void run() {
            LayoutInflater myInflator = getLayoutInflater();
            View myLayout = myInflator.inflate(R.layout.custom_layout,
                    (ViewGroup) findViewById(R.id.toastlayout));
            TextView myMessage = (TextView) myLayout
                    .findViewById(R.id.label);
            myMessage.setText(message);
            Toast toast = new Toast(getApplicationContext());
            toast.setView(myLayout);
            toast.setDuration(100);
            myMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL
                    | Gravity.CENTER_VERTICAL);
            toast.show();
        }
    });
}

0
投票
public void showMyToast(final Toast toast, final int delay) {
    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            toast.show();
        }
    }, 0, 1000);
    new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            toast.cancel();
            timer.cancel();
        }
    }, delay);
}

//如何使用(MainActivity.this是示例,它可以由您自己更改)和(在showMyToast(第一个参数,第二个参数)方法中,第二个参数是您真正定义的时间,如此)

Toast toast=Toast.makeText(MainActivity.this, "MyToast Test", Toast.LENGTH_LONG);
            showMyToast(toast, 1000);

-4
投票

这似乎对我有用(将持续时间设置为你想要的任何东西):

mActivity.runOnUiThread(new Runnable() {
                    public void run() {
                        Toast toast = new Toast(mActivity
                                .getApplicationContext());
                        toast.setView(layout);
                        toast.setDuration(400);
                        toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
                        toast.show();
                    }
                });
© www.soinside.com 2019 - 2024. All rights reserved.