android – 标题中带复选框的自定义对话框

android – 标题中带复选框的自定义对话框,第1张

概述我试图通过DialogFragment创建一个自定义对话框. 我想要一个标题和一个复选框. 我这样做是通过使用自定义标题,如下所示 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_wid 我试图通过DialogFragment创建一个自定义对话框.
我想要一个标题和一个复选框.

我这样做是通过使用自定义标题,如下所示

<?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"    androID:paddingtop="5dp" >    <TextVIEw        androID:ID="@+ID/textVIEw1"                androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_weight="1"        androID:paddingleft="15dp"        androID:text="CategorIEs"        androID:textcolor="#ffffff"        androID:textSize="22sp" />    <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" androID:paddingRight="10dp"/></linearLayout>

现在我想引用此复选框并向CheckBox添加一个监听器.

我希望实现这样的目标

我试过了什么?

public class CategorIEsDialogFragment extends SherlockDialogFragment {    ListVIEw dialog_ListVIEw;    static CheckBox chk_all;    static SelectVIEwHolder vIEwHolder;    private static ArrayAdapter<mItems> listadapter;    static ArrayList<String> checked = new ArrayList<String>();    protected static CharSequence[] _categorIEs = { "Amusement Park","Bird Sanctuary","Wild life","River","Hill Station","Temple","Rafting","Fishing","Hiking","Museums" };    protected static boolean[] _selections = new boolean[_categorIEs.length];    Placeslistadapter adapter;    ListVIEw ListVIEw;    button dialog_ok;    static int TAG = 0;    static mItems categorIEs;    static mItems orig;    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) {        final Dialog dialog = new Dialog(MainActivity.context);        //dialog.requestwindowFeature(Window.FEATURE_NO_Title);        dialog.setTitle("CategorIEs");        dialog.setContentVIEw(R.layout.dialog);        dialog_ok = (button) dialog.findVIEwByID(R.ID.button_category_ok);        dialog_ok.setonClickListener(new OnClickListener() {            public voID onClick(VIEw v) {                dialog.dismiss();            }        });        dialog_ListVIEw = (ListVIEw) dialog.findVIEwByID(R.ID.ListVIEwDialog);        dialog_ListVIEw                .setonItemClickListener(new AdapterVIEw.OnItemClickListener() {                    @OverrIDe                    public voID onItemClick(AdapterVIEw<?> parent,VIEw item,int position,long ID) {                        categorIEs = listadapter.getItem(position);                        orig = listadapter.getItem(position);                        categorIEs.toggleChecked();                        vIEwHolder = (SelectVIEwHolder) item.getTag();                        vIEwHolder.getCheckBox().setChecked(                                categorIEs.isChecked());                        if (!vIEwHolder.getCheckBox().isChecked()) {                            TAG = 1;                            chk_all.setChecked(false);                        }                        TAG = 0;                        /*                         * if (vIEwHolder.getCheckBox().isChecked()) {                         *                          * TAG = 0; }                         */                        for (int i = 0; i < _categorIEs.length; i++) {                            categorIEs = listadapter.getItem(i);                            if (!categorIEs.isChecked()) {                                break;                            }                            if (i == _categorIEs.length - 1) {                                TAG = 1;                                chk_all.setChecked(true);                                TAG = 0;                            }                        }                    }                });        chk_all = (CheckBox) dialog.findVIEwByID(R.ID.checkBoxAll);        chk_all.setonCheckedchangelistener(new OnCheckedchangelistener() {            @OverrIDe            public voID onCheckedChanged(Compoundbutton buttonVIEw,boolean isChecked) {                if (TAG != 1) {                    if (isChecked) {                        for (int i = 0; i < listadapter.getCount(); i++) {                            categorIEs = listadapter.getItem(i);                            categorIEs.setChecked(true);                        }                        listadapter.notifyDataSetChanged();                    } else {                        for (int i = 0; i < listadapter.getCount(); i++) {                            categorIEs = listadapter.getItem(i);                            categorIEs.setChecked(false);                        }                        listadapter.notifyDataSetChanged();                    }                }                if (TAG == 1) {                    TAG = 0;                }            }        });        // itemss = (mItems[]) onRetainNonConfigurationInstance();        ArrayList<mItems> categoryList = new ArrayList<mItems>();        categoryList.add(new mItems("Amusement Park"));        categoryList.add(new mItems("Bird Sanctuary"));        categoryList.add(new mItems("Wild life"));        categoryList.add(new mItems("River"));        categoryList.add(new mItems("Hill Station"));        categoryList.add(new mItems("Temple"));        categoryList.add(new mItems("Rafting"));        categoryList.add(new mItems("Fishing"));        categoryList.add(new mItems("Hiking"));        categoryList.add(new mItems("Museums"));        // Set our custom array adapter as the ListVIEw's adapter.        listadapter = new SelectArralAdapter(MainActivity.context,categoryList);        dialog_ListVIEw.setAdapter(listadapter);        return dialog;    }    private static class SelectArralAdapter extends ArrayAdapter<mItems> {        private LayoutInflater inflater;        public SelectArralAdapter(Context context,List<mItems> planetList) {            super(context,R.layout.dialog_row,R.ID.rowTextVIEw,planetList);            // Cache the LayoutInflate to avoID asking for a new one each            // time.            inflater = LayoutInflater.from(context);        }        @OverrIDe        public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {            // Planet to display            mItems planet = (mItems) this.getItem(position);            // The child vIEws in each row.            CheckBox checkBox;            TextVIEw textVIEw;            // Create a new row vIEw            if (convertVIEw == null) {                convertVIEw = inflater.inflate(R.layout.dialog_row,null);                // Find the child vIEws.                textVIEw = (TextVIEw) convertVIEw                        .findVIEwByID(R.ID.rowTextVIEw);                checkBox = (CheckBox) convertVIEw.findVIEwByID(R.ID.CheckBox01);                // Optimization: Tag the row with it's child vIEws,so we                // don't                // have to                // call findVIEwByID() later when we reuse the row.                convertVIEw.setTag(new SelectVIEwHolder(textVIEw,checkBox));                // If CheckBox is toggled,update the planet it is tagged                // with.                checkBox.setonClickListener(new VIEw.OnClickListener() {                    public voID onClick(VIEw v) {                        System.out.println("uffff");                        CheckBox cb = (CheckBox) v;                        mItems row_vIEw = (mItems) cb.getTag();                        row_vIEw.setChecked(cb.isChecked());                        TAG = 1;                        chk_all.setChecked(false);                        TAG = 0;                        for (int i = 0; i < _categorIEs.length; i++) {                            row_vIEw = listadapter.getItem(i);                            if (!row_vIEw.isChecked()) {                                break;                            }                            if (i == _categorIEs.length - 1) {                                TAG = 1;                                chk_all.setChecked(true);                                TAG = 0;                            }                        }                    }                });            }            // Reuse existing row vIEw            else {                // Because we use a VIEwHolder,we avoID having to call                // findVIEwByID().                SelectVIEwHolder vIEwHolder = (SelectVIEwHolder) convertVIEw                        .getTag();                checkBox = vIEwHolder.getCheckBox();                textVIEw = vIEwHolder.getTextVIEw();            }            // Tag the CheckBox with the Planet it is displaying,so that we            // can            // access the planet in onClick() when the CheckBox is toggled.            checkBox.setTag(planet);            // display planet data            checkBox.setChecked(planet.isChecked());            textVIEw.setText(planet.getname());            return convertVIEw;        }    }}

我不确定如何为自定义对话框包含setCustomTitle布局

解决方法

Now i want to reference this this Check Box and add a Listener to the
CheckBox.

我假设你使用DialogFragment的onCreateDialog方法显示对话框,所以在onCreateDialog方法中你会这样做:

// inflate the layout TitleVIEw customTitle = inflater.inflate(R.layout.custom_Title,null);// find the CheckBox and set the ListenerCheckBox ckb = (CheckBox) customTitle.findVIEwByID(R.ID.checkBox1);ckb.setonCheckedchangelistener(new ...);// set the Title vIEw on the dialog etc
总结

以上是内存溢出为你收集整理的android – 标题中带复选框的自定义对话框全部内容,希望文章能够帮你解决android – 标题中带复选框的自定义对话框所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1124779.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存