我試圖在從一個標籤切換到另一個標籤時「刪除」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