【发布时间】:2013-12-20 19:42:49
【问题描述】:
我在 android 应用程序中使用了 toasts。我可以显示 toast,而不是
Toast.LENGTH_LONG
和
Toast.LENGTH_SHORT
谁能帮我一些有用的代码段。谢谢。
【问题讨论】:
标签: android android-toast
我在 android 应用程序中使用了 toasts。我可以显示 toast,而不是
Toast.LENGTH_LONG
和
Toast.LENGTH_SHORT
谁能帮我一些有用的代码段。谢谢。
【问题讨论】:
标签: android android-toast
您可以做的是创建一种方法,该方法通过某种循环来使您的 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。
【讨论】:
试试这个..
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();
享受..
【讨论】:
tag.cancel() 在 onFinish() 里面?
不能直接处理,你必须像这样使用处理程序来取消吐司:
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.
【讨论】: