自定义对话框与setMultiChoiceItems [英] Custom Dialog with setMultiChoiceItems

查看:1882
本文介绍了自定义对话框与setMultiChoiceItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

您也许可以使用<一个href="http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setCustomTitle%28android.view.View%29"><$c$c>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).

这篇关于自定义对话框与setMultiChoiceItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆