这是我必须显示Toast
500毫秒的方式。虽然,它显示了不止一秒。如何显示特定持续时间的吐司?
Toast.makeText(LiveChat.this, "Typing", 500).show();
如何仅在500毫秒内显示Toast
?
这是我必须显示Toast
500毫秒的方式。虽然,它显示了不止一秒。如何显示特定持续时间的吐司?
Toast.makeText(LiveChat.this, "Typing", 500).show();
如何仅在500毫秒内显示Toast
?
这不能做。要显示短于Toast.LENGTH_SHORT
的长度,您必须在您想要的时间后取消它。例如:
final Toast toast = Toast.makeText(getApplicationContext(), "This message will disappear in half a second", Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 500);
感谢您的tips.Its工作正常。非常感谢。 – MuraliGanesan
@Muraliganesan请接受答案,如果它解决了您的问题 –
[这可以肯定!](http://stackoverflow.com/a/20373743/726863) –
我不相信这是可以做到的,你只能使用Toast.LENGTH_LONG
或Toast.LENTH_SHORT
你不能定义你的速度。
Toast.makeText(LiveChar.this,"Typing",Toast.LENGTH_SHORT);
这是你唯一的办法..
不能做你所要求的标准敬酒。也许你应该考虑整合一个第三方库,为你提供更好的Toast选项(名为Crouton)。我自己并没有使用它,但人们似乎很喜欢它。
您无法控制标准OS中Toast的长度。
这不能做。 Toast.LENGTH_SHORT
和Toast.LENGTH_LONG
的值是0和1.这意味着它们被视为标志而不是实际持续时间,所以我认为不可能将持续时间设置为除这些值之外的任何值。
先试一下。这设置以毫秒为单位的特定时段的烤面包:
public void toast(int millisec, String msg) {
Handler handler = null;
final Toast[] toasts = new Toast[1];
for(int i = 0; i < millisec; i+=2000) {
toasts[0] = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toasts[0].show();
if(handler == null) {
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toasts[0].cancel();
}
}, millisec);
}
}
}
添加'2000'到'我'不会确保时间已经过去..你需要使用时钟代替 –
你不能指望添加'2000'到'我'会添加'2秒',这取决于设备的速度。 如果你想循环直到需要做一些事情,比如'while((System.currentTimeMillis() - startTime)<2000))'。 这在每种编程语言中都是基本的。从来没有不同的循环时间间隔。 –
我找到了this answer。尽管有点复杂,它也可以让你创建比Toast.LENGTH_LONG长的敬酒。您可能不得不将刻度持续时间从1000毫秒更改为500毫秒。
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();
}
下面是它的工作原理:在倒数通知的时间比其土司根据标志显示的持续时间短,所以烤面包可以再次显示,如果倒计时还没有结束。如果烤面包仍然在屏幕上时再次显示,它将在整个持续时间内保持不闪烁。倒计时完成后,即使显示时间未结束,取消操作也会隐藏。
即使烤面包必须显示的时间短于默认持续时间,也能正常工作:倒计时完成后,第一个烤面包的显示将简单地取消。
这对我来说工作得很好。
final Toast mToastToShow;
int toastDurationInMilliSeconds = 10000;
mToastToShow = Toast.makeText(getApplicationContext(), "Snapshot Saved Successfully.",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();
倒计时用于显示特定持续时间的吐司消息。
添加到@ Senth的答案,如果你不惯于当你调用showToast方法多次,相同的消息累积时间:
private Toast mToastToShow = null;
String messageBeingDisplayed = "";
/**
* Show Toast message for a specific duration, does not show again if the message is same
*
* @param message The Message to display in toast
* @param timeInMSecs Time in ms to show the toast
*/
public void showToast(String message, int timeInMSecs) {
if (mToastToShow != null && message == messageBeingDisplayed) {
Log.d("DEBUG", "Not Showing another Toast, Already Displaying");
return;
} else {
Log.d("DEBUG", "Displaying Toast");
}
messageBeingDisplayed = message;
// Set the toast and duration
int toastDurationInMilliSeconds = timeInMSecs;
mToastToShow = Toast.makeText(this, message, Toast.LENGTH_LONG);
// Set the countdown to display the toast
CountDownTimer toastCountDown;
toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, timeInMSecs /*Tick duration*/) {
public void onTick(long millisUntilFinished) {
if (mToastToShow != null) {
mToastToShow.show();
}
}
public void onFinish() {
if (mToastToShow != null) {
mToastToShow.cancel();
}
// Making the Toast null again
mToastToShow = null;
// Emptying the message to compare if its the same message being displayed or not
messageBeingDisplayed = "";
}
};
// Show the toast and starts the countdown
mToastToShow.show();
toastCountDown.start();
}
可以为500毫秒像现在显示敬酒这样的:
showToast("Not Allowed", 500);
我尝试了不同的方法,这方法对我的作品
final Toast mytoast = Toast.makeText(getApplicationContext(), jsonObject.getString("response_message"), Toast.LENGTH_SHORT);
mytoast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mytoast.cancel();
}
}, 5000);// 5 sec
阅读文档。最后一个参数只能使用预定义的值,而不是毫秒。 – njzk2
[只要需要,您可以显示吐司](http://stackoverflow.com/a/20373743/726863) –
[可以Android的Toast比Toast.LENGTH \ _LONG长吗?](http ://stackoverflow.com/questions/2220560/can-an-android-toast-be-longer-than-toast-length-long) –