Android 设置吐司外观长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3775074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:05:57  来源:igfitidea点击:

Set Toast Appear Length

androidtimetoast

提问by Mitchell

Is there anyway I can tell a Toast Notification to show up only for a specified amount of time. Generally shorter then a regular toast message.

无论如何,我可以告诉 Toast 通知仅在指定的时间内显示。通常比普通的吐司消息更短。

回答by noypiscripter

I found a solution to this by calling toast.cancel() after a certain delay that is shorter than the standard toast duration.

我通过在比标准吐司持续时间短的特定延迟后调用 toast.cancel() 找到了解决方案。

        final Toast toast = Toast.makeText(ctx, "This message will disappear in 1 second", Toast.LENGTH_SHORT);
        toast.show();

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

回答by Macarse

No.

不。

You can do something like:

您可以执行以下操作:

Toast a = Toast.makeText(this, "a", Toast.LENGTH_LONG);
a.setDuration(300);

but it will not show itself.

但它不会显示自己。

The duration should be either LENGTH_SHORTor LENGTH_LONG.

持续时间应为LENGTH_SHORTLENGTH_LONG

回答by Virag Brahme

Try this

尝试这个

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

Hope this help.. Enjoy..!!!

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

回答by Skip

You can set a longer duration by using a hack, as described here

您可以通过使用一个黑客建立一个持续时间较长,如描述在这里

回答by Emran Hamza

//try it

//尝试一下

    public void myToast(String message) {
    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(400);
    myMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL
            | Gravity.CENTER_VERTICAL);
    toast.show();
}

回答by John P.

The stock Android Toast class is coded to accept only a Toast.LENGTH_SHORT or Toast.LENGTH_LONG parameter when calling a Toast. The values of these parameters are 0 and 1 respectively and do not accept any millisecond value when calling setDuration(); If you must show a Toast for a different duration than you may consider using a class from my SuperToastslibrary. The SuperToastclass in the library is a mimic of the stock Android Toast class and can have any millisecond value used as a duration parameter. I do not recommend using this class to show a Toast longerthan the maximum stock Android Toast length due to the lingering effect of these Toasts. I recommend that you use the SuperActivityToastclass to show Toast messages in an Activity/Fragment because the Toast will be destroyed along with your Activity eliminating any chance of a lingering message. To use this class you may create a new object:

股票 Android Toast 类被编码为在调用 Toast 时仅接受 Toast.LENGTH_SHORT 或 Toast.LENGTH_LONG 参数。这些参数的值分别为0和1,调用setDuration()时不接受任何毫秒值;如果您必须在不同的持续时间内展示 Toast,您可以考虑使用我的SuperToasts库中的类。库中的SuperToast类是对股票 Android Toast 类的模仿,可以将任何毫秒值用作持续时间参数。我不推荐使用这个类来说明吐司小于最大普通的Android吐司长度由于这些敬酒挥之不去的影响。我建议你使用SuperActivityToast类在 Activity/Fragment 中显示 Toast 消息,因为 Toast 将与您的 Activity 一起被销毁,从而消除了任何挥之不去的消息的机会。要使用此类,您可以创建一个新对象:

SuperActivityToast superActivityToast = new SuperActivityToast(this);  
superActivityToast.setDuration(SuperToast.DURATION_SHORT); 
// setDuration(); can also accept millisecond values
// superActivityToast.setDuration(1000);  
superActivityToast.setText("Hello world!");  
superActivityToast.show();  

Or use the static method:

或者使用静态方法:

SuperActivityToast.createDarkSuperActivityToast(this, "Hello world!", SuperToast.DURATION_SHORT).show();  

There are tons of customization options you can use with the library as well, check out the Wikipage!

您还可以在库中使用大量自定义选项,请查看Wiki页面!

回答by Dilip Muthukurussimana

Here is another way to configure the time per your choice:

这是根据您的选择配置时间的另一种方法:

public void showMsg(String msg, final long duration) {
    final Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
    toast.show();
    Thread t = new Thread() {
        public void run(){
            try {
                sleep(duration);
                toast.cancel(); 
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            finally { }
        }
    };
    t.start();
}

NOTE: The duration is specified in milliseconds.

注意:持续时间以毫秒为单位指定。