我的活动中有一个切换按钮。当开关为"开"时,它显示启用对话框,开关为"关闭",它显示禁用对话框。
((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 状态。预期结果是当按下取消时,切换按钮应保持在其原始位置,即,如果切换为打开,则在按下取消时应保持打开状态。我将如何实现这种操作?
另外,如果我在下面做一些事情,则按取消时会不断弹出两个对话框。
if(((SwitchCompat)findViewById(R.id.toggle3)).isChecked()){
((SwitchCompat)findViewById(R.id.toggle3)).setChecked(false);
}else {
((SwitchCompat)findViewById(R.id.toggle3)).setChecked(true);
}
你必须知道:
- onCheckedChanged 在 SwichCompat 的状态更改后调用。
- 如果您将 OnCheckedChangeListener 添加到 SwitchCompat 中,则此侦听器将在切换状态发生任何更改后触发(通过代码或手动按下(
因此,快速解决此问题,在将 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);
<</div>
div class="one_answers">else {
/*---todo---*/
dialog.cancel();
}
在上面的每个 else 代码上,表示您的案例中落在"Else"上的取消操作 - 您可以添加
((SwitchCompat)findViewById(R.id.toggle3)).setChecked(false);
或
((SwitchCompat)findViewById(R.id.toggle3)).setChecked(true);
在/---todo---/注释下方,就在你 dialog.cancel(( 之前;