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

|| 我想显示的吐司少于Toast.LENGTH_SHORT,因为我感觉它大约需要2秒钟。我只想显示吐司半秒钟。 Toast.LENGTH_SHORT和Toast.LENGTH_LONG的时间间隔是多少?     
已邀请:
只有两个可能的值:
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);
在“ 3”的来源中写道:   这段时间可以由用户定义。 但我找不到办法。 更新:这里有解决方案:设置吐司出现长度     
这对我有用
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);
    
在这里查看我建议的解决方案。基本上,在指定的延迟时间(比标准的吐司持续时间短)之后,基本上调用toast.cancel()。     
尝试这个
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();
希望能对您有所帮助。     
对于菜鸟,我提出了最简单的解决方案-方法。
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()。祝开发人员好运!     
它会工作。
   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();
        }
    });
}
    
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。这是示例,可以自己更改)  和(在showMyToast(first param,second param)方法中,第二个参数是您真正定义的时间,例如  这个)
Toast toast=Toast.makeText(MainActivity.this, \"MyToast Test\", Toast.LENGTH_LONG);
            showMyToast(toast, 1000);
    
这似乎对我有用(将持续时间设置为您想要的任何时间):
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();
                    }
                });
    

要回复问题请先登录注册