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

前端 未结 3 395
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 03:25

I have the following code:

private Toast movieRecordToast;

    private void displayNextMovie() {
        if (movieRecordToast != null) movieRecordToast.canc         


        
3条回答
  •  自闭症患者
    2020-12-19 04:17

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

提交回复
热议问题