【问题标题】:Multiple instances from clicking multiple times of the same button before it's launched在启动之前多次单击同一按钮的多个实例
【发布时间】:2013-01-03 14:49:32
【问题描述】:

有时当我的应用程序运行速度较慢时,用户可以多次单击启动意图的按钮。我听说这可能是 ICS 中修复的错误 - 是真的吗?或者我是否必须为我开始的每一个意图管理启动模式。 感谢您的帮助。

【问题讨论】:

  • 感谢您的回复。设置正确的启动模式似乎比弄乱按钮更简单。如果没有其他选择,我猜 android-manifest 活动启动模式中的“singleTop”就可以了。

标签: android android-4.0-ice-cream-sandwich android-4.2-jelly-bean


【解决方案1】:

添加一些简单的去抖动:

class MyActivity extends Activity {
    private boolean wasButtonPressed = false;

void onResume() {
    super onResume();
    wasButtonPressed = false;
}

void onClick( View view ) {
    if( !wasButtonPressed ) {
        wasButtonPressed = true;
        startActivity( ....
    }
 }

【讨论】:

    【解决方案2】:

    您必须在执行长时间操作时禁用该按钮,并在执行操作以响应用户点击后使其启用。您正在对 onClick 方法执行长时间操作,操作系统将堆叠您的所有用户点击,witch 将被异步转发,这不是错误。您可以显示进度对话框以通知用户长时间操作。一个快速的解决方法是禁用该按钮,直到上一次点击完全被消耗。

    @Override
        public void onClick(View aView) {
            int id = aView.getId();
            switch (id) {
                case R.id.yourButton:
                    aView.setEnabled(false);
                   // add your own implementation
                    aView.setEnabled(true);
                    break;
    
                default:
    
                    break;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      相关资源
      最近更新 更多