转到Android中的另一个屏幕时,停止所有吐司消息 [英] Stop all toast messages when going to another screen in Android

查看:82
本文介绍了转到Android中的另一个屏幕时,停止所有吐司消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击按钮时,我正在显示一个简单的Toast.我的问题是,当我多次单击一个按钮时,Toast消息一直显示直到我进入主屏幕.我想在进入主屏幕时停止Toast,并在相应的活动中杀死Toast消息.我已经附上了屏幕截图.

I am displaying a simple Toast when I click a button. My issue is, when I click on a button multiple times, that Toast message keeps displaying until I get to the main screen. I would like to stop the Toast when I get to the main screen and kill the Toast message in the corresponding activity. I have attached a screenshot.

我写的代码如下:

public class Main extends Activity {

    Dialog d;
    Toast t;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                t = Toast.makeText(Main.this, "you clicked on button!", Toast.LENGTH_LONG);
                t.show();
            }
        });
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        t.cancel();
    }
}

我该怎么办?

推荐答案

我也遇到了同样的问题.问题是吐司面包重叠,例如如果按10次,则Toast将保持10 x LENGTH_SHORT.我唯一的解决方案是控制自己表演Toast的时间.当您显示Toast时,只需跟踪上次显示它的时间,它就仍在屏幕上,不再显示.在最坏的情况下,吐司仅显示LENGTH_SHORT时间.

Hi I have the same problem. The problem is that the Toast overlaps e.g. if you press 10 times the Toast will stay 10 x LENGTH_SHORT. The only solution I came with was to control the time the Toast is shown by myself. When you show a Toast just keep a track of the last time you show it, it's still on the screen don't show it again. In your worst case the Toast will be visible only LENGTH_SHORT time.

这篇关于转到Android中的另一个屏幕时,停止所有吐司消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆