gpt4 book ai didi

android - AlertDialog MultiChoiceItems - 一次只选择 2 个项目

转载 作者:太空宇宙 更新时间:2023-11-03 10:20:11 26 4
gpt4 key购买 nike

我的警报对话框中有 4 个复选框:A、B、C 和 D。我正在尝试让 A 只能用 B、C 或 D 选择,所以只有 2 个被选中一度。

是否有能够实现此行为的解决方案?我已经查看了我可以通过谷歌找到的内容,但我找不到任何东西。我想我需要使用共享首选项,然后单击 B、C 和 D 中的每一个后,我可以在 SP 中存储一个字符串,引用 B、C、D 中的每一个,然后它可以检查 SP 中的字符串每次单击一个时,如果从 B、C 或 D 中存储的值之一在那里,请取消选中已勾选的复选框(希望这是有意义的),或者是否有更好的解决方案?谢谢

我目前拥有的:

boolean[] checked = new boolean[] { true, false, false, false }; //This is defined at the top of my class, so that once the dialog is first opnened, A is selected by default

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.filteroptions, menu);
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.filter:

AlertDialog dialog;

final CharSequence[] items = { "A", "B", "C", "D" };
// arraylist to keep the selected items
final ArrayList<Integer> seletedItems = new ArrayList<Integer>();

shfObject = getActivity().getSharedPreferences("NAME",
Context.MODE_PRIVATE);
final SharedPreferences.Editor shfEditorObject = shfObject.edit();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Filter");



builder.setMultiChoiceItems(items, checked,
new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog,
int indexSelected, boolean isChecked) {

if (isChecked) {
seletedItems.add(indexSelected);

switch (indexSelected) {

case 0:
shfEditorObject.putString(
"checkbox1Ticked", "Ticked1");

shfEditorObject.commit();
break;

case 1:

shfEditorObject.putString(
"checkbox2Ticked", "Ticked2");

shfEditorObject.commit();

break;

case 2:

shfEditorObject.putString(
"checkbox3Ticked", "Ticked3");

shfEditorObject.commit();

break;

case 3:

shfEditorObject.putString(
"checkbox4Ticked", "Ticked4");

shfEditorObject.commit();

break;

}

}

else if (seletedItems.contains(indexSelected)) {
// Else, if the item is already in the
// array, remove it
// write your code when user Uchecked the
// checkbox

}

}
})
// Set the action buttons
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
// Your code when user clicked on OK
// You can write the code to save the
// selected item here

}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
// Your code when user clicked on Cancel

}
});

dialog = builder.create();// AlertDialog dialog; create like this
// outside onClick
dialog.show();

return true;

default:
break;
}

return true;
}

最佳答案

您应该在检查每个项目时更新计数器。当达到最大限制时,您可以禁用其他复选框。

这个问题很好地回答了- How to disable checkbox when a limit is reached in android?

关于android - AlertDialog MultiChoiceItems - 一次只选择 2 个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24586396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com