设为首页 加入收藏

TOP

Android开发笔记
2019-09-03 03:44:31 】 浏览:40
Tags:Android 开发 笔记

1.使用AlertDialog.Builder 对话框自定义view,并通过setview设置

 AlertDialog.Builder dlgAlert;
            dlgAlert = new AlertDialog.Builder(this);
            LayoutInflater inflater = getLayoutInflater();

            dlgAlert.setTitle("用户协议");
            //dlgAlert.setMessage(R.string.agreement);
            View checkView=inflater.inflate(R.layout.agreedialogview,null);
            dlgAlert.setView(checkView);
            CheckBox agreeCheck=(CheckBox)checkView.findViewById(R.id.checkBox_agree);;
            dlgAlert.setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, close current activity
                            if (agreeCheck.isChecked()) {
                                init();
                            }
                            else
                            {
                                finish();
                                System.exit(0);
                            }
                        }
                    }).create();

            dlgAlert.setNeutralButton("退出",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, close current activity
                            finish();
                            System.exit(0);
                        }
                    }).create();
            dlgAlert.show();

这里要想在对话框按钮的监听事件中调用xml布局里面的控件,不能直接findViewById,需要这样写

View checkView=inflater.inflate(R.layout.agreedialogview,null);
            dlgAlert.setView(checkView);
            CheckBox agreeCheck=(CheckBox)checkView.findViewById(R.id.checkBox_agree);;

对话框.show()函数之后才可以调用,

 

 

 

 

编程之路,坑多且长,此处留白,未完待续

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Okhttp3源码解析(5)-拦截器RetryA.. 下一篇Andriod安卓下开发UHF读写器

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目