3

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

((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);
}
4

2 回答 2

3

你必须知道:

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

因此,快速解决此问题,在您将 swich set 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);
于 2017-04-06T08:55:23.190 回答
0

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

在上面的每一个 else 代码上,表示取消操作落在您的案例的“else”上 - 您可以添加

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

或者

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

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

于 2017-04-06T08:47:46.540 回答