如何使用android中的警告对话框构建器实现多选选择器

时间:2012-10-22 12:22:54

标签: android

任何人都可以告诉如何使用android

中的警告对话框构建器实现多选选择器

由于

2 个答案:

答案 0 :(得分:1)

很简单,请遵循以下教程:

http://labs.makemachine.net/2010/03/android-multi-selection-dialogs/

简而言之:

new AlertDialog.Builder( this )
               .setTitle( "Planets" )
               .setMultiChoiceItems( new String[]{"Mars", "Earth"}, new boolean[]{false, false}, new DialogSelectionClickHandler() )
               .setPositiveButton( "OK", new DialogButtonClickHandler() )
               .create();

答案 1 :(得分:0)

    btnHost = (Button) view.findViewById(R.id.btnHost);
    btnHost.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // ((TextView)((RelativeLayout)getActivity().getLayoutInflater().inflate(R.layout.event_filter_title,
            // null)).findViewById(R.id.tvDialogTitle)).setText("Hosts");

            // Log.d( "Dialog object",
            // " static made dialog in checkbox--> "+((TextView)((RelativeLayout)getActivity().getLayoutInflater().inflate(R.layout.event_filter_title,
            // null)).findViewById(R.id.tvDialogTitle)).getText());

            final LayoutInflater inflater123 = (LayoutInflater) getActivity()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            final View view123 = inflater123.inflate(
                    R.layout.event_filter_title, null);

            // Log.d( "Dialog object",
            // " static made dialog in view123--> "+view123);
            //
            // Log.d( "Dialog object",
            // " static made dialog in checkbox--> "+((CheckBox)view123.findViewById(R.id.cbSelectAll)));
            //
            // Log.d( "Dialog object",
            // " static made dialog in checkbox--> "+((CheckBox)inflater.inflate(R.layout.event_filter_title,
            // (ViewGroup) ((AlertDialog)
            // BeepsFragment.dialog).getCurrentFocus(),
            // true).findViewById(R.id.cbSelectAll)));
            //
            // Log.d( "Dialog object",
            // " static made dialog in checkbox--> "+((CheckBox)(((RelativeLayout)getActivity().getLayoutInflater().inflate(R.layout.event_filter_title,
            // null)).findViewById(R.id.cbSelectAll))));

            hostsDialog = new AlertDialog.Builder(getActivity())
                    .setCustomTitle(/*
                                     * Html.fromHtml(
                                     * "<b><font color=\"purple\">  Host</font></b>"
                                     * )
                                     */view123)
                    .setIcon(
                            getActivity().getResources().getDrawable(
                                    R.drawable.add_host))
                    .setMultiChoiceItems(hostsStringArray, isSelectedHosts,
                            new OnMultiChoiceClickListener() {

                                // android.content.DialogInterface.OnShowListener
                                // ocl = new
                                // DialogInterface.OnShowListener() {
                                //
                                // @Override
                                // public void onShow(DialogInterface
                                // dialog) {
                                // // TODO Auto-generated method stub
                                // BeepsFragment.dialog = dialog;
                                // }
                                // };

                                public void onClick(DialogInterface dialog,
                                        int clicked, boolean selected) {

                                    boolean all = true;
                                    Log.i("ME", hosts.toArray()[clicked]
                                            + " selected: " + selected);

                                    // for (int i = 0; i <
                                    // isSelectedHosts.length; i++ ){
                                    //
                                    // if(isSelectedHosts[i]==true){
                                    // all = true;
                                    // }else{
                                    // all = false;
                                    // }
                                    // }
                                    //
                                    // Log.i( "ME", all + " selected:--- "
                                    // +((CheckBox)view123.findViewById(R.id.cbSelectAll)));
                                    //
                                    // if(all = true){
                                    // ((CheckBox)view123.findViewById(R.id.cbSelectAll)).setChecked(true);
                                    // }else{
                                    // ((CheckBox)view123.findViewById(R.id.cbSelectAll)).setChecked(false);
                                    // }

                                    Log.d("Dialog object",
                                            " static made dialog --> "
                                                    + BeepsFragment.dialog
                                                    + " from parameter dialog --> "
                                                    + dialog);

                                }

                            })
                    .setPositiveButton(
                            Html.fromHtml("<b><font color=\"purple\">Apply Filter</font></b>"),
                            new DialogButtonClickHandler() {

                                // android.content.DialogInterface.OnShowListener
                                // ocl = new
                                // DialogInterface.OnShowListener() {
                                //
                                // @Override
                                // public void onShow(DialogInterface
                                // dialog) {
                                // // TODO Auto-generated method stub
                                // BeepsFragment.dialog = dialog;
                                // }
                                // };

                                public void onClick(DialogInterface dialog,
                                        int clicked) {
                                    switch (clicked) {
                                    case DialogInterface.BUTTON_POSITIVE:

                                        filteredList.clear();
                                        hostsSelected.clear();

                                        for (int i = 0; i < hostsStringArray.length; i++) {
                                            Log.i("ME",
                                                    hosts.toArray()[i]
                                                            + " selected: "
                                                            + isSelectedHosts[i]
                                                            + "\n\n\t hostStringArray-->"
                                                            + hostsStringArray[i]);

                                            if (isSelectedHosts[i] == true) {

                                                hostsSelected.add(hosts
                                                        .get(i));
                                                isSelectedHosts[i] = false;
                                            }
                                            // isSelectedHosts[i] = false;
                                        }

                                        Calendar currentCalender = Calendar
                                                .getInstance(Locale
                                                        .getDefault());

                                        Date currentDate = new Date(
                                                currentCalender
                                                        .getTimeInMillis());

                                        if (listSelected == 0) {
                                            for (int j = 0; j < arr_BLID
                                                    .size(); j++) {
                                                if (Helper
                                                        .stringToDate(
                                                                arr_BLID.get(
                                                                        j)
                                                                        .getStart_ts()
                                                                        .toString(),
                                                                Helper.SERVER_FORMAT)
                                                        .after(currentDate)) {
                                                    if (hostsSelected
                                                            .contains(arr_BLID
                                                                    .get(j)
                                                                    .getHost_name())) {
                                                        filteredList
                                                                .add(arr_BLID
                                                                        .get(j));
                                                    }
                                                    if (hostsSelected
                                                            .contains("Me"))
                                                        if (BeepApplication
                                                                .getSelfId() == arr_BLID
                                                                .get(j)
                                                                .getHost_id())
                                                            filteredList
                                                                    .add(arr_BLID
                                                                            .get(j));
                                                }
                                            }

                                        } else {
                                            for (int j = 0; j < arr_BLID
                                                    .size(); j++) {
                                                if (currentDate.after(Helper
                                                        .stringToDate(
                                                                arr_BLID.get(
                                                                        j)
                                                                        .getStart_ts()
                                                                        .toString(),
                                                                Helper.SERVER_FORMAT))) {
                                                    if (hostsSelected
                                                            .contains(arr_BLID
                                                                    .get(j)
                                                                    .getHost_name())) {
                                                        filteredList
                                                                .add(arr_BLID
                                                                        .get(j));
                                                    }
                                                    if (hostsSelected
                                                            .contains("Me"))
                                                        if (BeepApplication
                                                                .getSelfId() == arr_BLID
                                                                .get(j)
                                                                .getHost_id())
                                                            filteredList
                                                                    .add(arr_BLID
                                                                            .get(j));
                                                }
                                            }

                                        }
                                        lvBeeps.setAdapter(new EventsAdapter(
                                                ctx));
                                        break;
                                    }
                                }

                            })
                    .setNegativeButton(
                            Html.fromHtml("<b><font color=\"purple\">Remove Filter</font></b>"),
                            new DialogButtonClickHandler() {

                                public void onClick(
                                        final DialogInterface dialog,
                                        int clicked) {

                                    Calendar currentCalender = Calendar
                                            .getInstance(Locale
                                                    .getDefault());

                                    Date currentDate = new Date(
                                            currentCalender
                                                    .getTimeInMillis());

                                    if (listSelected == 0) {

                                        filteredList.clear();
                                        for (int i = 0; i < arr_BLID.size(); i++) {
                                            if (Helper.stringToDate(
                                                    arr_BLID.get(i)
                                                            .getStart_ts()
                                                            .toString(),
                                                    Helper.SERVER_FORMAT)
                                                    .after(currentDate)) {
                                                filteredList.add(arr_BLID
                                                        .get(i));
                                                if (i < isSelectedHosts.length) {
                                                    isSelectedHosts[i] = false;
                                                } else {
                                                    continue;
                                                }
                                            }
                                        }

                                        lvBeeps.setAdapter(new EventsAdapter(
                                                ctx));
                                    } else {

                                        filteredList.clear();
                                        for (int i = 0; i < arr_BLID.size(); i++) {
                                            if (currentDate.after(Helper
                                                    .stringToDate(
                                                            arr_BLID.get(i)
                                                                    .getStart_ts()
                                                                    .toString(),
                                                            Helper.SERVER_FORMAT))) {
                                                filteredList.add(arr_BLID
                                                        .get(i));
                                                if (i < isSelectedHosts.length) {
                                                    isSelectedHosts[i] = false;
                                                } else {
                                                    continue;
                                                }
                                            }
                                        }

                                        lvBeeps.setAdapter(new EventsAdapter(
                                                ctx));

                                    }
                                }
                            });

            final AlertDialog dlg = hostsDialog.create();

            dlg.show();

            ((TextView) view123.findViewById(R.id.tvDialogTitle))
                    .setText("Hosts");

            ((CheckBox) view123.findViewById(R.id.cbSelectAll))
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(
                                CompoundButton buttonView, boolean isChecked) {
                            // TODO Auto-generated method stub

                            if (isChecked == true) {

                                ListView list = dlg.getListView();
                                for (int i = 0; i < list.getCount(); i++) {

                                    isSelectedHosts[i] = true;
                                    list.setItemChecked(i, true);

                                }

                            }

                            else if (isChecked == false) {

                                ListView list = dlg.getListView();
                                for (int j = 0; j < list.getCount(); j++) {

                                    isSelectedHosts[j] = false;
                                    list.setItemChecked(j, false);
                                }
                                // }
                            }

                        }
                    });

        }
    });