我在我的android应用程序中使用了toasts 。我可以随心所欲地展示吐司,而不是
Toast.LENGTH_LONG
和
Toast.LENGTH_SHORT
有人可以帮我一些有用的代码段。谢谢你。
我在我的android应用程序中使用了toasts 。我可以随心所欲地展示吐司,而不是
Toast.LENGTH_LONG
和
Toast.LENGTH_SHORT
有人可以帮我一些有用的代码段。谢谢你。
您可以做的是创建一种方法,该方法通过某种循环来使您的 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();
享受..
no 不能直接处理,你必须使用 handler 来取消 toast,如下所示:
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.