单击 - Android后,警报对话框自动关闭

时间:2013-11-04 05:15:26

标签: android android-alertdialog android-custom-view baseadapter

我正在开发我的第一个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;
        }
    }

我做错了什么?

1 个答案:

答案 0 :(得分:0)

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

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

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