如何停止显示来自吐司消息,当应用程序被关闭?吐司、应用程序、消息

2023-09-06 13:41:09 作者:似梦

这是我的样本code:

 公共类MainActivity延伸活动{

    按钮buttonClick;
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        buttonClick =(按钮)findViewById(R.id.buttonClick);
        buttonClick.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                Toast.makeText(MainActivity.this,这里是吐司,Toast.LENGTH_SHORT).show();
            }
        });
    }

}
 

当过我按一下按钮,将显示敬酒消息。这很好。

我游戏打不开,提示已停止工作,出现了一个问题,导致程序停止正常工作,请关闭该程序

我的问题是:

当我点击的 多次 的按钮,然后关闭应用程序,然后在敬酒继续开始他们的显示信息的工作。

我不希望出现这种情况。

我想,如果应用程序被关闭,然后敬酒也应该停止显示他们的消息。

谁能告诉我,我必须做这个???

解决方案   

我想,如果应用程序被关闭,然后烤面包也应该停止显示该消息。

在你的情况下调用取消()吐司对象在的onDestroy取消()方法。

下面是一个类似的例子。

已更新!

我测试OP解决方案,但没有结果。

.hide() .cancel()方法可用于吐司,但似乎他们没有工作。解决的办法是,你必须创建它就像一个你自己的自定义视图吐司,然后你可以取消所有敬酒活动结束。

This is my sample code:

public class MainActivity extends Activity {

    Button buttonClick;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonClick = (Button) findViewById(R.id.buttonClick);
        buttonClick.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "Here is the Toast", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

When ever I click on the Button, the Toast message is displayed. That's fine.

My question is:

When I click multiple times on the button and then close the application, then the Toasts continue starting their work of displaying messages.

And I don't want that.

I want if the application is closed then Toasts should also stop displaying their messages.

Can anybody tell me what I have to do for this???

解决方案

I want if the application is closed then Toast should also stop displaying the message.

In your case call cancel() to Toast object to cancel it within onDestroy() method.

Here is a similar example.

Updated!

I tested OP solution but no result.

.hide() and .cancel() method is available for Toast but seem they are not working. The solution is, you have to create your own custom view which acts like a Toast and then you can cancel all Toasts when the Activity finishes.