我一直在使用一个普通的"myToast“,在发布新吐司之前我使用的是"myToast.cancel()
”。对于Androidv2.3和更高版本,这很好。当需要发送一个新的吐司时,旧的吐司(如果仍然在屏幕上)会被取消(并立即消失),被替换为新的吐司。如果用户多次按下需要提醒(和其他条件)的键,那么就避免了堆叠一堆祝酒词。我的实际情况是,当按错键时会出现一个吐司,如果没有按下清晰键,则会出现另一个吐司。
对于Android4.0和4.1,在下一次吐司之前发出一个myToast.cancel()
将同时杀死当前和下一个吐司。当前的cancel()
API确实表明它取消了当前和下一个吐司(这似乎相当愚蠢)。你为什么要取消祝酒词?
对于让cancel在Android版本中一致工作(以及它在2.3和更高版本中的工作方式),有什么想法吗?
我将尝试一些不雅致的双重吐司系统,并跟踪使用吐司的情况,但在4.x中,这种糟糕的行为似乎让人感到痛苦,才能在旧的Android版本中完美地、合乎逻辑地工作。
好吧,我解决了,但它没有我想要的那么干净。我实施了一种双重吐司方法,在两种祝酒词之间交替使用。首先,我们为OnCreate
之前的活动定义祝酒词。
Toast toast0;
Toast toast1;
private static boolean lastToast0 = true;
在OnCreate中:
toast0 = new Toast(getApplicationContext());
toast0.cancel();
toast1 = new Toast(getApplicationContext());
toast1.cancel();
最后,当我需要显示祝酒词并同时取消先前的祝酒词时,我使用类似于:
if (lastToast0) {
toast0.cancel();
toast1.setDuration(Toast.LENGTH_LONG);
toast1.setText("new message");
toast1.show();
lastToast0 = false;
} else {
toast1.cancel();
toast0.setDuration(Toast.LENGTH_LONG);
toast0.setText("new message");
toast0.show();
lastToast0 = true;
}
如果您需要取消现有的吐司(在超时之前),请使用:
toast0.cancel();
toast1.cancel();
在Nexus 7 (4.1)、仿真器4.0和几个使用Android2.2、2.3的设备上进行了测试。
发布于 2012-10-16 20:28:25
而不是调用cancel()
。尝试重置文本并调用show()
。这会自动取消最后的祝酒词。
myToast.setText("wrong key")
myToast.show();
如果您继续使用相同的myToast
,而不是每次创建一个,我猜它们不会增加。
发布于 2012-11-16 22:33:26
南德什的解决方案对你不管用吗?他的解决方案比用两种不同的祝酒词更干净。
例如,(详述他/她的答案)在onCreate之前,我们将宣布祝酒词:
private Toast myToast;
在onCreate中,我们必须使用makeToast初始化它(否则我们会得到一个错误):
myToast = Toast.makeText(getApplicationContext(), null, Toast.LENGTH_SHORT);
每当我们想要祝酒词的时候,我们只需打电话:
myToast.setText("some text");
myToast.show();
这将适当地取代之前的祝酒词。
发布于 2013-04-19 08:04:11
以下是我从另一个类似问题中抄袭的答案:
Boast
类完全实现了所需的功能。
诀窍是跟踪显示的最后一个Toast
,并取消那个。
我所做的就是创建一个Toast
包装器,它包含对最后显示的Toast的静态引用。
当我需要显示一个新引用时,首先取消静态引用,然后显示新引用(并将其保存在静态中)。
下面是我制作的Boast
包装器的完整代码--它模仿了足够多的Toast方法,可以让我使用它。默认情况下,Boast
将取消前一个命令,因此不会建立等待显示的Toasts队列。
如果您只是想知道如何取消通知时退出您的应用程序,你会发现很多帮助在那里。
package mobi.glowworm.lib.ui.widget;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.Nullable;
import android.widget.Toast;
import java.lang.ref.WeakReference;
/**
* {@link Toast} decorator allowing for easy cancellation of notifications. Use this class if you
* want subsequent Toast notifications to overwrite current ones. </p>
* <p/>
* By default, a current {@link Boast} notification will be cancelled by a subsequent notification.
* This default behaviour can be changed by calling certain methods like {@link #show(boolean)}.
*/
public class Boast {
/**
* Keeps track of certain Boast notifications that may need to be cancelled. This functionality
* is only offered by some of the methods in this class.
* <p>
* Uses a {@link WeakReference} to avoid leaking the activity context used to show the original {@link Toast}.
*/
@Nullable
private volatile static WeakReference<Boast> weakBoast = null;
@Nullable
private static Boast getGlobalBoast() {
if (weakBoast == null) {
return null;
}
return weakBoast.get();
}
private static void setGlobalBoast(@Nullable Boast globalBoast) {
Boast.weakBoast = new WeakReference<>(globalBoast);
}
// ////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Internal reference to the {@link Toast} object that will be displayed.
*/
private Toast internalToast;
// ////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Private constructor creates a new {@link Boast} from a given {@link Toast}.
*
* @throws NullPointerException if the parameter is <code>null</code>.
*/
private Boast(Toast toast) {
// null check
if (toast == null) {
throw new NullPointerException("Boast.Boast(Toast) requires a non-null parameter.");
}
internalToast = toast;
}
// ////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Make a standard {@link Boast} that just contains a text view.
*
* @param context The context to use. Usually your {@link android.app.Application} or
* {@link android.app.Activity} object.
* @param text The text to show. Can be formatted text.
* @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or
* {@link Toast#LENGTH_LONG}
*/
@SuppressLint("ShowToast")
public static Boast makeText(Context context, CharSequence text, int duration) {
return new Boast(Toast.makeText(context, text, duration));
}
/**
* Make a standard {@link Boast} that just contains a text view with the text from a resource.
*
* @param context The context to use. Usually your {@link android.app.Application} or
* {@link android.app.Activity} object.
* @param resId The resource id of the string resource to use. Can be formatted text.
* @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or
* {@link Toast#LENGTH_LONG}
* @throws Resources.NotFoundException if the resource can't be found.
*/
@SuppressLint("ShowToast")
public static Boast makeText(Context context, int resId, int duration)
throws Resources.NotFoundException {
return new Boast(Toast.makeText(context, resId, duration));
}
/**
* Make a standard {@link Boast} that just contains a text view. Duration defaults to
* {@link Toast#LENGTH_SHORT}.
*
* @param context The context to use. Usually your {@link android.app.Application} or
* {@link android.app.Activity} object.
* @param text The text to show. Can be formatted text.
*/
@SuppressLint("ShowToast")
public static Boast makeText(Context context, CharSequence text) {
return new Boast(Toast.makeText(context, text, Toast.LENGTH_SHORT));
}
/**
* Make a standard {@link Boast} that just contains a text view with the text from a resource.
* Duration defaults to {@link Toast#LENGTH_SHORT}.
*
* @param context The context to use. Usually your {@link android.app.Application} or
* {@link android.app.Activity} object.
* @param resId The resource id of the string resource to use. Can be formatted text.
* @throws Resources.NotFoundException if the resource can't be found.
*/
@SuppressLint("ShowToast")
public static Boast makeText(Context context, int resId) throws Resources.NotFoundException {
return new Boast(Toast.makeText(context, resId, Toast.LENGTH_SHORT));
}
// ////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Show a standard {@link Boast} that just contains a text view.
*
* @param context The context to use. Usually your {@link android.app.Application} or
* {@link android.app.Activity} object.
* @param text The text to show. Can be formatted text.
* @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or
* {@link Toast#LENGTH_LONG}
*/
public static void showText(Context context, CharSequence text, int duration) {
Boast.makeText(context, text, duration).show();
}
/**
* Show a standard {@link Boast} that just contains a text view with the text from a resource.
*
* @param context The context to use. Usually your {@link android.app.Application} or
* {@link android.app.Activity} object.
* @param resId The resource id of the string resource to use. Can be formatted text.
* @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or
* {@link Toast#LENGTH_LONG}
* @throws Resources.NotFoundException if the resource can't be found.
*/
public static void showText(Context context, int resId, int duration)
throws Resources.NotFoundException {
Boast.makeText(context, resId, duration).show();
}
/**
* Show a standard {@link Boast} that just contains a text view. Duration defaults to
* {@link Toast#LENGTH_SHORT}.
*
* @param context The context to use. Usually your {@link android.app.Application} or
* {@link android.app.Activity} object.
* @param text The text to show. Can be formatted text.
*/
public static void showText(Context context, CharSequence text) {
Boast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
/**
* Show a standard {@link Boast} that just contains a text view with the text from a resource.
* Duration defaults to {@link Toast#LENGTH_SHORT}.
*
* @param context The context to use. Usually your {@link android.app.Application} or
* {@link android.app.Activity} object.
* @param resId The resource id of the string resource to use. Can be formatted text.
* @throws Resources.NotFoundException if the resource can't be found.
*/
public static void showText(Context context, int resId) throws Resources.NotFoundException {
Boast.makeText(context, resId, Toast.LENGTH_SHORT).show();
}
// ////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Close the view if it's showing, or don't show it if it isn't showing yet. You do not normally
* have to call this. Normally view will disappear on its own after the appropriate duration.
*/
public void cancel() {
internalToast.cancel();
}
/**
* Show the view for the specified duration. By default, this method cancels any current
* notification to immediately display the new one. For conventional {@link Toast#show()}
* queueing behaviour, use method {@link #show(boolean)}.
*
* @see #show(boolean)
*/
public void show() {
show(true);
}
/**
* Show the view for the specified duration. This method can be used to cancel the current
* notification, or to queue up notifications.
*
* @param cancelCurrent <code>true</code> to cancel any current notification and replace it with this new
* one
* @see #show()
*/
public void show(boolean cancelCurrent) {
// cancel current
if (cancelCurrent) {
final Boast cachedGlobalBoast = getGlobalBoast();
if ((cachedGlobalBoast != null)) {
cachedGlobalBoast.cancel();
}
}
// save an instance of this current notification
setGlobalBoast(this);
internalToast.show();
}
}
https://stackoverflow.com/questions/12922516
复制相似问题