我有一个带有复选框的对话框。在选择此复选框之前,我想询问用户,他一定要选择这些复选框。 (我的意思是,我尝试选择复选框,然后在此 AlertDialog 出现之前询问我是否确定)
代码如下:
CheckBox cb = (CheckBox) v.findViewById(R.id.favouriteChkBox);
if (cb.isChecked()) {
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.setTitle("Do you want to add this to favourite?");
builder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
itemChecked.set(position, true);
}
});
builder.setNegativeButton("no",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
builder.create().show();
}
但这不起作用。显示警报对话框,无论我单击"is"还是“否”,复选框都会被选中。
你能告诉我为什么这不起作用吗? 谢谢!
最佳答案
final CheckBox cb = (CheckBox) findViewById(R.id.favouriteChkBox);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (cb.isChecked()) {
AlertDialog.Builder builder = new AlertDialog.Builder(
TestGitActivity.this);
builder.setTitle("Do you want to add this to favourite?");
builder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
cb.setChecked(true);
}
});
builder.setNegativeButton("no",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
cb.setChecked(false);
}
});
builder.create().show();
}
}
});
关于android - 在 Android 中选择复选框之前的 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15318784/