【问题标题】:controling the displaying time of a toast [duplicate]控制吐司的显示时间[重复]
【发布时间】:2013-12-20 19:42:49
【问题描述】:

我在 android 应用程序中使用了 toasts。我可以显示 toast,而不是

Toast.LENGTH_LONG

Toast.LENGTH_SHORT

谁能帮我一些有用的代码段。谢谢。

【问题讨论】:

标签: android android-toast


【解决方案1】:

您可以做的是创建一种方法,该方法通过某种循环来使您的 Toast 显示只要您想要的持续时间,

private void showToast(int duration) {
    final Toast toast = Toast.makeText(getBaseContext(), 
                                                      "This is a Toast Message!",
                Toast.LENGTH_SHORT);
    toast.show();
    new CountDownTimer(duration, 500) {
            public void onTick(long millisUntilFinished) {
                toast.show();
            }
            public void onFinish() {
                toast.cancel();
            }

        }.start();
    }

然后您可以将此方法称为showToast(10000);。因此,它将继续循环显示 Toast 直到持续时间,并在持续时间完成时取消 toast。

【讨论】:

    【解决方案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();
    

    享受..

    【讨论】:

    • 您确定它会显示 10 秒吗?我不这么认为:-)
    • 是的..它会出现10秒
    • @Lalit 请尝试编辑的答案.. 它的工作原理
    • 也许你的意思是 tag.cancel()onFinish() 里面?
    • 是的,你的权利.. 为错误道歉......另外,如果它正在工作,你可以接受答案
    【解决方案3】:

    不能直接处理,你必须像这样使用处理程序来取消吐司:

     final Toast toast = Toast.makeText(getApplicationContext(), "This 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); // 500ms is the time to cancel the toast.
    

    【讨论】:

    • 并没有真正回答 OP 的问题...
    • 虽然这段代码可以工作,但它仍然可能对 OP 没有帮助,因为他想要更长的持续时间。
    猜你喜欢
    • 2019-11-19
    • 1970-01-01
    • 2014-05-13
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    相关资源
    最近更新 更多