我在创建的自定义对话框中有一个复选框。 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/