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

android – AlertDialog MultiChoiceItems – 一次只选择2个项目,第1张

概述我的警报对话框中有4个复选框:A,B,C和D.我试图获得使其成为A只能用B,C或D选择的行为,因此一次只能选择2个. 是否有解决方案能够实现这种行为?我看过我能通过谷歌找到什么,但我找不到任何东西.我想我将需要使用共享首选项,然后一旦点击B,C和D中的每一个,我可以在SP中存储一个字符串,引用每个B,C,D,然后它可以检查SP中的字符串每次单击一个,如果存在从B,C或D中存储的值之一,则取消选中已 我的警报对话框中有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
}; //This is defined at the top of my class,so that once the dialog is first opnened,A is selected by default

@OverrIDepublic voID onCreateOptionsMenu(Menu menu,MenuInflater inflater) {    inflater.inflate(R.menu.filteroptions,menu);    super.onCreateOptionsMenu(menu,inflater);}@OverrIDepublic 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",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个项目全部内容,希望文章能够帮你解决android – AlertDialog MultiChoiceItems – 一次只选择2个项目所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1125262.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存