【问题标题】:Restore state of toggle button on canceling dialog在取消对话框上恢复切换按钮的状态
【发布时间】:2017-04-06 08:21:10
【问题描述】:

我的活动中有一个切换按钮。开关打开时显示启用对话框,开关关闭时显示禁用对话框。

((SwitchCompat) findViewById(R.id.toggleButton)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                ((StyleableTextView) findViewById(R.id.lbl_toggle_status)).setText("on");
                showEnableSharingDialog();
            } else {
                ((StyleableTextView) findViewById(R.id.lbl_toggle_status)).setText("off");
                showDiasableShareingDialog();
            }
        }
    });

这是启用对话框:

private void showEnableShareingDialog() {
    final CharSequence options[]= getResources().getStringArray(R.array.sharing_options);
    AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);

     builder.setTitle(getString(R.string.you_want_to_enable_prev_sigtings));

    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if(which==0){
                mShareSigtings=true;
                mEnableShareing=true;
                updateSettings();
            } else if(which==1){
                mShareSigtings=true;
                mEnableShareing=false;
                updateSettings();
            } else {
                /*---todo---*/
                dialog.cancel();
            }
        }
    });
    builder.show();
}

这是禁用对话框:

private void showDiasableShareingDialog() {
    final CharSequence options[]= getResources().getStringArray(R.array.remove_sharing_options);
    AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);

    builder.setTitle(getString(R.string.you_want_to_remove_prev_sigtings));

    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == 0) {
                mShareSigtings = false;
                mRemovePrevSigtings = true;
                updateSettings();
            } else if (which == 1) {
                mShareSigtings = false;
                mRemovePrevSigtings = false;
                updateSettings();
            } else {
                /*---todo---*/
                dialog.cancel();
            }
        }
    });
    builder.show();
}

在两个对话框中,切换按钮切换它的状态,即说切换处于 ON 状态,然后我按下按钮,对话框弹出,当我按下 CANCEL 时,它从 ON 状态变为 OFF 状态。预期结果是当按下 CANCEL 时,切换按钮应保持在其原始位置,即,如果切换为 ON,则在按下 CANCEL 时应保持 ON。我要如何实现这种操作?

此外,如果我执行以下操作,则两个对话框都会在按下 CANCEL 时不断弹出。

if(((SwitchCompat)findViewById(R.id.toggle3)).isChecked()){
                    ((SwitchCompat)findViewById(R.id.toggle3)).setChecked(false);
}else {
                    ((SwitchCompat)findViewById(R.id.toggle3)).setChecked(true);
}

【问题讨论】:

  • 当您设置开关切换时,您的 setOnChangeListener 条件会导致问题。当通过 .setChecked 以编程方式切换开关的代码被调用时,它会被触发。

标签: android android-togglebutton switchcompat


【解决方案1】:

你必须知道:

  • onCheckedChanged 在 SwichCompat 的状态更改后调用。
  • 如果您在 SwitchCompat 中添加了 OnCheckedChangeListener,则此侦听器将在开关状态发生任何变化后触发(通过代码或手动按下)

因此,快速解决此问题,在您将 swich 设置 null 的检查状态更改为 onCheckedChanged 之前,然后再次放置侦听器。这是一个例子:

 SwitchCompat mySwich = ((SwitchCompat) findViewById(R.id.toggleButton))   
     OnCheckedChangeListener listener = new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        ((StyleableTextView) findViewById(R.id.lbl_toggle_status)).setText("on");
                        showEnableSharingDialog();
                    } else {
                        ((StyleableTextView) findViewById(R.id.lbl_toggle_status)).setText("off");
                        showDiasableShareingDialog();
                    }
                }
            };
mySwitch.setOnCheckedChangeListener(listener);

在您的 CANCEL 回调中,您可以执行以下操作:

mySwitch.setOnCheckedChangeListener(null);
if(((SwitchCompat)findViewById(R.id.toggle3)).isChecked()){
                    ((SwitchCompat)findViewById(R.id.toggle3)).setChecked(false);
}else {
                    ((SwitchCompat)findViewById(R.id.toggle3)).setChecked(true);
}
mySwitch.setOnCheckedChangeListener(listener);

【讨论】:

  • 感谢帮助解决了我的问题
【解决方案2】:

else {
   /*---todo---*/
   dialog.cancel();
}

在上面的每一个 else 代码上,表示取消操作,对于您的情况,该操作落在“else”上 - 您可以添加

((SwitchCompat)findViewById(R.id.toggle3)).setChecked(false);

((SwitchCompat)findViewById(R.id.toggle3)).setChecked(true);

在 /---todo---/ 评论下方,就在您之前 dialog.cancel();

【讨论】:

  • 我必须为两个对话框或其中任何一个对话框添加它吗?
  • @Raj 尝试一个或两个。
  • 我按照你说的尝试过,结果它在其中一种情况下失败,具体取决于你把它放在哪里,对话框也会显示两次
猜你喜欢
  • 2017-08-29
  • 1970-01-01
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 2011-06-16
相关资源
最近更新 更多