How to cancel Toast created in a different method on android?

ⅰ亾dé卋堺 提交于 2019-11-28 03:14:34

问题


I have the following 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();        

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.


回答1:


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.

Sample 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();
}



回答2:


I think there are many ways you can achieve displaying the next/prev info to the user. I would ditch the toasts altogether and update the text of a TextView with the name of next/prev movie. That would eliminate your problem and also IMHO makes for better UI.

However, if your design requirements do ask for toast notifications, try:

    private Toast nextMovieRecordToast;
private Toast prevMovieRecordToast;


private void displayNextMovie() {
    if (prevMovieRecordToast != null) prevMovieRecordToast.cancel(); // cancel previous Toast (if user changes movies too often)
    nextMovieRecordToast = Toast.makeText(getApplicationContext(), "Next", Toast.LENGTH_SHORT);
    nextMovieRecordToast.show();}

private void displayPrevMovie() {
    if (nextMovieRecordToast != null) nextMovieRecordToast.cancel();
    prevMovieRecordToast = Toast.makeText(getApplicationContext(), "Prev", Toast.LENGTH_SHORT);
    prevMovieRecordToast.show();    }    



回答3:


The wroclai's solution is excellent! However it screws the Toast when going form long message toast to short one and vice versa. To fix this instead of using previous object recreate it. So instead of this line:
mToastText.setText(message);
write this one: myToast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
The animations also looks better :)



来源:https://stackoverflow.com/questions/5503682/how-to-cancel-toast-created-in-a-different-method-on-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!