如何防止活动在按下按钮时加载两次 [英] How to prevent the activity from loading twice on pressing the button

查看:34
本文介绍了如何防止活动在按下按钮时加载两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在第一次点击后立即按下按钮两次,我试图阻止活动加载两次.

I am trying to prevent the activity from loading twice if I press the button twice instantly after the first click.

我有一个点击按钮加载的活动,比如

I have an activity which loads on click of a button, say

 myButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
       //Load another activity
    }
});

现在因为要加载的activity有网络调用,加载需要一点时间(MVC).我确实为此显示了一个加载视图,但是如果我在此之前按两次按钮,我可以看到活动被加载了两次.

Now because the activity to be loaded has network calls, it takes a little time to load (MVC). I do show a loading view for this but if I press the button twice before that, I can see the activity being loaded twice.

有谁知道如何防止这种情况发生?

Do any one know how to prevent this?

推荐答案

在按钮的事件侦听器中,禁用按钮并显示另一个活动.

In the button's event listener, disable the button and show another activity.

    Button b = (Button) view;
    b.setEnabled(false);

    Intent i = new Intent(this, AnotherActitivty.class);
    startActivity(i);

覆盖 onResume() 以重新启用按钮.

Override onResume() to re-enable the button.

@Override
    protected void onResume() {
        super.onResume();

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setEnabled(true);
    }

这篇关于如何防止活动在按下按钮时加载两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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