0

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

alert.getListView().setEnabled(false);

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

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();

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

4

3 回答 3

2

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

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;
    }

}
于 2013-11-06T09:56:42.927 回答
0

我遇到了同样的问题并以这种方式解决了它:

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();

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

于 2014-05-14T10:18:59.077 回答
0

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

builder.setMultiChoiceItems(对话框列表,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()
}
于 2021-09-30T07:05:12.537 回答