【问题标题】:Disabling CheckBox in MultiChoiceItems AlertDialog在 MultiChoiceItems AlertDialog 中禁用 CheckBox
【发布时间】:2023-03-21 03:56:01
【问题描述】:

我有一个AlertDialog,它有动态项目链接到数据库中的光标,它工作正常,但我想禁用用户交互,因为它是一个信息对话框,我已经禁用了我的对话框:

alert.getListView().setEnabled(false);

但问题是当列表中的项目大于对话框高度时,由于禁用它的 ListView,滚动也被禁用并且某些项目没有显示,我尝试了一些链接,例如:Android : How to disable CheckBox in AlertDialog? 没有成功。我也试过了:

builder.setMultiChoiceItems(mDBAdapter.instance.getName(SupervisorGuid)
                , SyncDBHelper.instance.SentSupervisors(SupervisorGuid),new DialogInterface.OnMultiChoiceClickListener() {

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

                            if(isChecked==true)
                            {

                                    ((AlertDialog) dialog).getListView().setItemChecked(which, false);
                            }
                            else
                            {
                                    ((AlertDialog) dialog).getListView().setItemChecked(which, true); 
                            }


                    }});
        AlertDialog alert = builder.create();

有没有人知道在没有自定义对话框的情况下禁用复选框的更好方法? 提前致谢。

【问题讨论】:

    标签: android checkbox dialog android-alertdialog multichoiceitems


    【解决方案1】:

    这可能会给你一些想法......

    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        ArrayList<String> arrayList = new ArrayList<String>();
        ArrayList<Boolean> selectedList = new ArrayList<Boolean>();
        for (int i = 1; i <= 20; i++) {
            arrayList.add("" + i);
            selectedList.add(i % 2 == 0);
        }
        builder.setAdapter(new MyAdapter(arrayList, selectedList), null);
        builder.show();
    }
    
    
     public static class MyAdapter extends BaseAdapter {
    
        private ArrayList<String> arrayList;
        private ArrayList<Boolean> selectedList;
    
        public MyAdapter(ArrayList<String> arrayList,
                ArrayList<Boolean> selectedList) {
            this.arrayList = arrayList;
            this.selectedList = selectedList;
        }
    
        @Override
        public int getCount() {
            return arrayList.size();
        }
    
        @Override
        public Object getItem(int position) {
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getView(parent.getContext(), position);
        }
    
        private RelativeLayout getView(Context context, int position) {
            RelativeLayout relativeLayout = new RelativeLayout(context);
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            relativeLayout.setLayoutParams(params);
            TextView textView = new TextView(context);
            textView.setText(arrayList.get(position));
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
            layoutParams.leftMargin = 30;
            textView.setLayoutParams(layoutParams);
            relativeLayout.addView(textView);
            CheckBox checkBox = new CheckBox(context);
            layoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams.rightMargin = 30;
            checkBox.setLayoutParams(layoutParams);
            checkBox.setClickable(false);
            if (selectedList != null) {
                checkBox.setChecked(selectedList.get(position));
            }
            relativeLayout.addView(checkBox);
            return relativeLayout;
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,用这种方法解决了:

      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      
      ...
      
      builder
          .setTitle(...)
          .setMultiChoiceItems(cursor, IS_CHECKED, LABEL, new DialogInterface.OnMultiChoiceClickListener() {
              @Override
                  public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                      ((AlertDialog)dialog).getListView().setItemChecked(which, /* assign here the original boolean value of the item */);
                  }
          })
      
      ...
      
      AlertDialog dialog = builder.create();
      dialog.show();
      

      总而言之,当您单击列表中的项目时,只需将其值设置回原始布尔值即可。

      【讨论】:

      • 应该可以默认做吗?没有任何点击?意思是当我们直接打开弹出窗口时它显示为点击?
      【解决方案3】:

      您可以使用布尔数组定义您的复选框值 "checkValues" 是我们的布尔数组,它定义了哪个复选框应该打勾或者哪个不打勾。检查下面的例子。

      builder.setMultiChoiceItems(dialogList, checkedValues)

          private fun showDialog() {
          val dialogList: Array<String> = days
          val checkedValues = BooleanArray(days.size)
          checkedValues[viewpager.currentItem] = true
          val builder: AlertDialog.Builder = AlertDialog.Builder(activity)
          
      
          builder.setTitle("You can copy"+dialogList[viewpager.currentItem]+" time slot to:")
          builder.setMultiChoiceItems(dialogList, checkedValues)
          { dialog, which, isChecked ->
              (dialog as AlertDialog).listView.setItemChecked(yourIndex, true)
      
          }.setPositiveButton("OK") { dialog, id ->
            
                  dialog.dismiss()
          }.setNegativeButton("Cancel") { dialog, id ->
                  dialog.dismiss()
          }.show()
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多