Thêm vào @ câu trả lời Senth, nếu bạn không wont tích lũy thời gian khi bạn gọi phương thức showToast nhiều lần, với cùng một thông điệp:
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();
}
Bạn có thể hiển thị bánh mì nướng bây giờ cho 500 ms như này:
showToast("Not Allowed", 500);
đọc doc. tham số cuối cùng chỉ có thể lấy các giá trị được xác định trước, nó không phải tính bằng milis. – njzk2
[Bạn có thể hiển thị bánh mì nướng miễn là cần thiết] (http://stackoverflow.com/a/20373743/726863) –
Bản sao có thể có của [Bánh nướng Android có thể dài hơn "Bánh mì nướng.LENGTH \ _LONG"?] (Http : //stackoverflow.com/questions/2220560/can-an-android-toast-be-longer-than-toast-length-long) –