我试图在从一个标签切换到另一个标签时“删除”Toast
,因此假设暂停焦点选项卡活动。基本上我可以将取消方法应用于单个Toast
对象,或将其分配给null。这工作。删除线程Toast消息
问题是,当我使用TextView和线程Toast时,即使停止线程,Toast仍然可见。我从this point开始线程吐司,并试图根据this one停止它。
如何停止线程并从系统队列中删除吐司,还是有更好的解决方案来实现该目标(例如,在单个活动中多次吐司,当从活动交换到另一个时停止显示吐司)?
调用吐司(这是OK):
private void showSingleToast(String toastText, int color, long duration_ms) {
if (mySingleToast instanceof Toast) {
mySingleToast.setText(toastText);
if (null != toastView)
if (toastView instanceof TextView)
toastView.setTextColor(color);
ToastExpander.showFor(mySingleToast, duration_ms);
}
}
穿线Toast.show()
public class ToastExpander extends Thread {
public static final String TAG = "ToastExpander";
static Thread t;// = null;
// Must be volatile:
static volatile boolean stopFlag = false;
static long scan_freq = 300;
public static void showFor(final Toast aToast, final long durationInMilliseconds) {
aToast.setDuration(Toast.LENGTH_SHORT);
t = new Thread() {
long timeElapsed = 0l;
public void run() {
try {
while (timeElapsed <= durationInMilliseconds || !stopFlag) {
long start = System.currentTimeMillis();
aToast.show();
sleep(scan_freq);
timeElapsed += System.currentTimeMillis() - start;
}
// doesn't work: aToast.cancel();
} catch (InterruptedException e) {
Log.e(TAG, e.toString());
}
} /* end of "run" */
}; /* end of Thread */
t.start();
} /* end showFor */
public static void cancel() {
stopFlag = true;
}
}
试图 “去除” 的吐司(不起作用)
private void hideSingleToast() {
//hideTextView(toastView);
toastView = null;
ToastExpander.cancel();
mySingleToast.cancel();
//mySingleToast= null;
}
我确认mySingleToast是在onCreate中创建的。 – hornetbzz