自定义对话框与setMultiChoiceItems自定义、对话框、setMultiChoiceItems

由网友(花念伊人泪)分享简介:我想创建一个方式,用户可以选择像下面I want to create a way users can select options like the image below现在我在做以下public static class CategoriesDialogFragment extends SherlockDial...

我想创建一个方式,用户可以选择像下面

I want to create a way users can select options like the image below

现在我在做以下

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

}

不过,我想补充,如图像中的所有选项。所以,我想我将不得不建立一个自定义对话框。我仍然可以扩展本机setMultiChoiceItems,这样它会降低我的code处理。

But i want to add ALL option like the image. So i think i will have to build a custom Dialog. Can i still extend the native setMultiChoiceItems so that it will reduce my handling of the code.

推荐答案

您也许可以使用setCustomTitle()在 AlertDialog.Builder 类的方法和构造自己的头衔有标题的文本,也是的所有的复选框,是这样的:

You could probably use the setCustomTitle() method of the AlertDialog.Builder class and construct your own title which has the title text and also the all CheckBox, something like this:

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

其中, 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>

其他风格的调整应以使其​​更好看。 但看到整个对话框的布局,你可能想要去与一个自定义的对话框类,其中 setMultiChoice()方法将不可用(但最终它会很容易复制)。

Other style tweaks should be made to make it look better. But seeing the entire dialog layout you may want to go with a custom Dialog class, for which the setMultiChoice() method will not be available(but in the end it will be easy to replicate).

阅读全文

相关推荐

最新文章