0

我正在开发我的第一个 Android 应用程序。我想显示一个带有自定义多选的对话框。我可以做到,但是当我单击一个项目时,对话框将被关闭,除非我再次调用 builder.show()。我的代码是:

      case R.id.menu_references:
                final boolean[] oldValue = checkedItems.clone();
                final AlertDialog.Builder referenceBuilder = new AlertDialog.Builder(this);
                referenceBuilder.setPositiveButton(context.getString(R.string.save), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
                        if (!Arrays.equals(checkedItems, oldValue)) {
                            saveSettings(oldValue, checkedItems);
                        }
                    }
                });
                final BaseAdapter adapter = new ReferencesAdapter(context, checkedItems);
                referenceBuilder.setAdapter(adapter, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        checkedItems[which] = !checkedItems[which];
                        adapter.notifyDataSetChanged();
                        referenceBuilder.show(); //IF I REMOVE THIS LINE, THE DIALOG IS BEING CLOSED
                    }
                });
                referenceBuilder.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
                        checkedItems = oldValue;
                        dialog.dismiss();
                    }
                });
                referenceBuilder.create().show();
                return true;

和适配器类:

public class ReferencesAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private boolean checkedItems[];
        private Context context;
        private SettingOption[] values;

        public ReferencesAdapter(Context context, boolean[] checkedItems) {
            super();
            this.checkedItems = checkedItems;
            inflater = LayoutInflater.from(context);
            this.values = SettingOption.values();
            this.context = context;
        }

        @Override
        public int getCount() {
            return SettingOption.values().length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null) {
                convertView = inflater.inflate(R.layout.activity_reference, null);
                holder = new ViewHolder();
                holder.check_title =(CheckedTextView)convertView.findViewById(R.id.setting_check);
                holder.check_image =(ImageView)convertView.findViewById(R.id.setting_image);
                convertView.setTag(holder);
            } else {
                holder =(ViewHolder)convertView.getTag();
            }
            SettingOption option = values[position];
            holder.check_title.setText(option.toString());
            holder.check_image.setBackgroundDrawable(context.getResources().getDrawable(option.getDrawable()));
            if(checkedItems[position]) {
                holder.check_title.setChecked(true);
            } else {
                holder.check_title.setChecked(false);
            }
            return convertView;
        }  

        class ViewHolder {
            CheckedTextView check_title;
            ImageView check_image;
        }
    }

我究竟做错了什么?

4

1 回答 1

0

如果您想捕获用户的响应并且不允许取消对话,您应该使用

final AlertDialog.Builder referenceBuilder = new AlertDialog.Builder(this);
referenceBuilder.setCancelable(false);

这样对话框就不会消失。除非用户按下某个对话框按钮。

于 2013-11-04T05:22:06.443 回答