是否有办法在30秒内显示Toast消息(没有闪烁效果?
我已经尝试过本文中的方法,但是这些方法都不适合我(Android 9)
Can an Android Toast be longer than Toast.LENGTH_LONG?
最重要的是,我无法根据自己的情况使用快餐栏或状态栏通知功能
你在这里!
private Toast mToastToShow;
public void showToast(View view) {
// Set the toast and duration
int toastDurationInMilliSeconds = 10000;
mToastToShow = Toast.makeText(this, "Hello world, I am a toast.", Toast.LENGTH_LONG);
// Set the countdown to display the toast
CountDownTimer toastCountDown;
toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, 1000 /*Tick duration*/) {
public void onTick(long millisUntilFinished) {
mToastToShow.show();
}
public void onFinish() {
mToastToShow.cancel();
}
};
// Show the toast and starts the countdown
mToastToShow.show();
toastCountDown.start();
}
您可以使用SuperToast向用户显示烤面包
以其他方式,您可以显示一个不可取消的对话框,并在30秒内将其置于线程中以将其关闭,但是您必须处理视图和上下文,所以这是非常困难的工作
更新:
您可以尝试使用此代码,可能对您有用:
private Toast mToastToShow;
public void showToast(View view) {
// Set the toast and duration
int toastDurationInMilliSeconds = 10000;
mToastToShow = Toast.makeText(this, "Hello world, I am a toast.",
Toast.LENGTH_LONG);
// Set the countdown to display the toast
CountDownTimer toastCountDown;
toastCountDown = new CountDownTimer(toastDurationInMilliSeconds,
1000 /*Tick duration*/) {
public void onTick(long millisUntilFinished) {
mToastToShow.show();
}
public void onFinish() {
mToastToShow.cancel();
}
};
// Show the toast and starts the countdown
mToastToShow.show();
toastCountDown.start();
}