我有几个SeekBar
和onSeekBarProgressStop()
,我想显示一个Toast
消息。
但是,如果我对SeekBar
迅速执行操作,则UI线程会以某种方式阻塞,并且Toast
消息会等到UI线程空闲为止。
现在,我担心的是如果Toast
消息已经显示,则避免使用新的Toast
消息。还是他们通过任何条件检查UI线程当前是否空闲,然后显示Toast
消息。
我通过使用runOnUIThread()
和创建新的Handler
两种方式进行了尝试。
最佳答案
我已经尝试了多种方法来做到这一点。最初,我尝试使用cancel()
,它对我没有任何影响(另请参阅this answer)。
有了setDuration(n)
,我也不到任何地方。通过记录getDuration()
可以发现它的值为0(如果makeText()
的参数为Toast.LENGTH_SHORT
)或1(如果makeText()
的参数为Toast.LENGTH_LONG
)。
最后,我尝试检查吐司的 View isShown()
。当然不是没有显示吐司,而是在这种情况下甚至返回致命错误。因此,我需要尝试捕获错误。
现在,如果显示吐司,isShown()
返回true。
利用isShown()
我想出了以下方法:
/**
* <strong>public void showAToast (String st)</strong></br>
* this little method displays a toast on the screen.</br>
* it checks if a toast is currently visible</br>
* if so </br>
* ... it "sets" the new text</br>
* else</br>
* ... it "makes" the new text</br>
* and "shows" either or
* @param st the string to be toasted
*/
public void showAToast (String st){ //"Toast toast" is declared in the class
try{ toast.getView().isShown(); // true if visible
toast.setText(st);
} catch (Exception e) { // invisible if exception
toast = Toast.makeText(theContext, st, toastDuration);
}
toast.show(); //finally display it
}