android - 复选框在 Android 中的对话框启动时取消选中

标签 android checkbox

我在创建的自定义对话框中有一个复选框。 onClick 函数似乎工作正常,但如果我关闭对话框并重新打开,该框将恢复为未选中状态。我假设某些东西正在刷新并且不记得复选框的状态。我怎样才能得到它来保存支票?

这是我的对话:

public Dialog onCreateDialog() {
     AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);

     LayoutInflater inflater = getLayoutInflater();
     View layout = inflater.inflate(R.layout.popuplayout, null);
     helpBuilder.setView(layout);

     helpBuilder.setPositiveButton(
       "OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Close the dialog
            }
        });
        AlertDialog helpDialog = helpBuilder.create();
        helpDialog.show();
        return helpBuilder.create();

}

复选框方法:

public void onCheckboxClicked(View view){
    boolean checked = ((CheckBox) view).isChecked();
    switch(view.getId()) {
        case R.id.check_devices:
            if(checked)
                abc();
            break;
    }
}

最佳答案

您仍然需要在创建对话框时将 CheckBox 设置为已选中。

编辑

您可以覆盖 onPrepareDialog,您可以在其中搜索 CheckBox(通过 findViewById(R.id.yourCheckBox)),然后调用 setChecked(boolean value ) 就可以了。

@Override
public void onPrepareDialog(int id, Dialog dialog){
    CheckBox cb = (CheckBox)findViewById(R.id.yourCheckBox);
    cb.setChecked(valueToSet); // Could be a global variable, or always true if that is what you need.

    super.onPrepareDialog(id, dialog);
}

请记住,这是一种已弃用的方法,但它传达了信息(我希望)。

关于android - 复选框在 Android 中的对话框启动时取消选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17215125/

相关文章:

android - 模拟惰性构造函数参数

checkbox - 在 golang 中检查复选框的惯用方式

android - RecyclerView 在 NotifyDataSetChanged() 后不更新,内容在打开和关闭屏幕后显示

android - 适用于 Android 的 Google Drive Web 服务 API - 显示文件夹中的文件名

android - getContactsFromFirebase() 方法返回一个空列表

android - 如何在 Android 中 10 秒后隐藏进度条?

c# - 在复选框上选中相应的表之间传输数据

javascript - 在点击事件处理程序触发之前更改复选框的状态是否存在错误?

JavaFX : Add CheckBoxTreeItem in TreeTable?

javascript - 选择所有复选框问题(jQuery)