设置复选框列表对话框的复选框
我有一个显示复选框列表的对话框。我想设置每次显示对话框时选中的不同框。但这仅在第一次有效。我希望每次显示对话框时它都有效!如果有人可以提供帮助,那就太好了...
这是我的代码:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case CHECKBOX_LIST_DIALOG:
final CharSequence[] weeks = new CharSequence[53];
for (int i=0; i<=52; i++) {
weeks[i] = String.valueOf(i+1);
}
return new AlertDialog.Builder(this).setTitle(
R.string.txt_week_checkbox_list).setMultiChoiceItems(
weeks, getCheckedBoxes(),
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
checked[whichButton] = isChecked;
}
}).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
EditText editText = (EditText) findViewById(R.id.edittext_weeks);
editText.setText(generateString());
}
}).setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
}).create();
}
I've got a dialog which shows a list of checkBoxes. I'd like to set different boxes checked each time the dialog is showed. But that only works the first time.. I want it work every time the dialog is showed! It would be great if anyone can help...
This is my code:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case CHECKBOX_LIST_DIALOG:
final CharSequence[] weeks = new CharSequence[53];
for (int i=0; i<=52; i++) {
weeks[i] = String.valueOf(i+1);
}
return new AlertDialog.Builder(this).setTitle(
R.string.txt_week_checkbox_list).setMultiChoiceItems(
weeks, getCheckedBoxes(),
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
checked[whichButton] = isChecked;
}
}).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
EditText editText = (EditText) findViewById(R.id.edittext_weeks);
editText.setText(generateString());
}
}).setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
}).create();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过
onCreateDialog()
创建的托管对话框会被缓存。您需要重写onPrepareDialog()
,以便您在下次显示对话框时获得控制权。您将传递Dialog
对象。将其转换为AlertDialog
,调用getListView()
,并使用setItemChecked()
打开或关闭每个复选框。Managed dialogs created via
onCreateDialog()
are cached. You will need to overrideonPrepareDialog()
, so you get control when the dialog is next shown. You are passed theDialog
object. Cast that to anAlertDialog
, callgetListView()
, and usesetItemChecked()
to toggle each checkbox on or off.伟大的!就这样搞定了,谢谢!!这正是我一直在寻找的:-)
这是我为使其像您所解释的那样工作所做的:
Great! That did it, thanks!! That was exactly what I was looking for :-)
Here's what I did to make it work like you explained: