使用setMultiChoiceItems的自定义对话框

Har*_*M V 9 android customdialog android-dialogfragment

我想创建一种方式,用户可以选择下面的图像选项

在此输入图像描述

现在我正在做以下事情

public static class CategoriesDialogFragment extends SherlockDialogFragment {

    public static CategoriesDialogFragment newInstance(int title) {
        CategoriesDialogFragment frag = new CategoriesDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");

        return new AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setMultiChoiceItems(_categories, _selections,
                        new DialogSelectionClickHandler())
                .setPositiveButton(R.string.alert_dialog_ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                ((MainActivity) getActivity())
                                        .doPositiveClick();
                            }
                        }).create();

        /*
         * .setNegativeButton(R.string.alert_dialog_cancel, new
         * DialogInterface.OnClickListener() { public void
         * onClick(DialogInterface dialog, int whichButton) {
         * ((MainActivity) getActivity()) .doNegativeClick(); } })
         */
    }

    public class DialogSelectionClickHandler implements
            DialogInterface.OnMultiChoiceClickListener {
        public void onClick(DialogInterface dialog, int clicked,
                boolean selected) {
            // Log.i("ME", _options[clicked] + " selected: " + selected);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

但我想添加像图像一样的所有选项.所以我想我将不得不建立一个自定义对话框.我是否仍然可以扩展本机setMultiChoiceItems,以便减少我对代码的处理.

Luk*_*rog 10

您可以使用类的setCustomTitle()方法AlertDialog.Builder并构建自己的标题,其中包含标题文本以及all CheckBox,如下所示:

new AlertDialog.Builder(getActivity())
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(title)
                .setCustomTitle(getLayoutInflater().inflate(R.layout.custom_title, null));
Run Code Online (Sandbox Code Playgroud)

在哪里R.layout.custom_title:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        style="?android:attr/textAppearanceLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Dialog title"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="All"
        android:textColor="#ffffff" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

应该进行其他样式调整以使其看起来更好.但是看到整个对话框布局,您可能希望使用自定义Dialog类,该setMultiChoice()方法将无法使用(但最终将很容易复制).