自定义控件(二)自定义Dialog

本节要实现:自定义一个Dialog 结果如下:

步 骤 1.配置register_dialog.xml: 下面是一个自定义的dialog。 功能是:点击dialog所依附的activity上的“注册”按钮,,弹出此对话框。

======”确认手机号码”android:textColor=”#000″/><TextView==”我们将发送验证码短信到这个号码:\n+86 XXXXXXXXXXX”android:textColor=”#000″/><LinearLayout=”40dp”android:orientation=”horizontal”><LinearLayout=”1″android:gravity=”center”><Button===”#fff”/></LinearLayout><LinearLayout=”1″android:gravity=”center”><Button===></LinearLayout>

2.Java代码示例: (这里只贴出和对话框有关的代码,即一个处理自定义Dialog的方法)

(AlertDialog.Builder builder){LayoutInflater inflater=getLayoutInflater();final View view=inflater.inflate(R.layout.register_dialog, null);//获取自定义对话框资源//获取自定义dialog下的确定和取消按钮资源Button okBtnInDialog=(Button)view.findViewById(R.id.register_dialog_ok_btn);Button cancelBtnInDialog=(Button)view.findViewById(R.id.register_dialog_cancel_btn);builder.setIcon(R.drawable.icon);builder.setView(view);builder.create();builder.show();/*** 下面为自定义dialog下的确定和取消按钮设置点击事件*/okBtnInDialog.setOnClickListener((View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setClass(Register.this,Vertify.class);startActivity(intent);MyThread thread = new MyThread();thread.start();}});cancelBtnInDialog.setOnClickListener((View arg0) {// TODO Auto-generated method stub}});}

值得注意的是:在获取Dialog上的button等资源时,使用的是view.findViewById(R.id.button)。这才表明你是在对话框View中寻找资源。倘若直接用findViewById(R.id.button),表明在此activity里找资源ID,能找到才怪!

木已成舟便要顺其自然

自定义控件(二)自定义Dialog

相关文章:

你感兴趣的文章:

标签云: