由网友(焚烧的香烟i)分享简介:我有以下的code:private Toast movieRecordToast;private void displayNextMovie() {if (movieRecordToast != null) movieRecordToast.cancel(); // cancel previous Toast (if...
我有以下的code:
private Toast movieRecordToast;
private void displayNextMovie() {
if (movieRecordToast != null) movieRecordToast.cancel(); // cancel previous Toast (if user changes movies too often)
movieRecordToast = Toast.makeText(getApplicationContext(), "Next", Toast.LENGTH_SHORT);
movieRecordToast.show();
private void displayPrevMovie() {
if (movieRecordToast != null) movieRecordToast.cancel();
movieRecordToast = Toast.makeText(getApplicationContext(), "Prev", Toast.LENGTH_SHORT);
movieRecordToast.show();
但是,如果 displayNextMovie
被称为快速数次,然后显示prevMovie
被称为下一个吐司仍显示,并显示$ P $光伏之后。
看起来注销不能正常工作。
But if displayNextMovie
is called quickly several times and then displayPrevMovie
is called, "Next" Toast is still shown and only after that "Prev" is displayed.
Looks like cancellation doesn't work properly.
推荐答案
而不是创建新的吐司
你想要一个新的文本显示,你可以轻松地抓住每一次对象只有一个吐司
对象,并取消当前的吐司
只要你想。在接下来的吐司
正在显示你可以改变文本 Toast.setText()
的功能。
Instead of creating a new Toast
object each time you want a new text displayed you can easily hold on to only one Toast
object and cancel the current Toast
whenever you want. Before the next Toast
is being displayed you can change text with Toast.setText()
function.
样品code:
private Toast mToastText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the object once.
mToastText = Toast.makeText(this, "", Toast.LENGTH_SHORT);
}
private void displayText(final String message) {
mToastText.cancel();
mToastText.setText(message);
mToastText.show();
}
相关推荐
最新文章