自己定义控件(二)自己定义Dialog
控件 自己 定义 Dialog
2023-09-27 14:25:13 时间
本节要实现:自己定义一个Dialog
结果例如以下:
步 骤
1.配置register_dialog.xml:
以下是一个自己定义的dialog。
功能是:点击dialog所依附的activity上的“注冊”button,弹出此对话框。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:text="确认手机号码"
android:textColor="#000"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="30dp"
android:text="我们将发送验证码短信到这个号码:\n+86 XXXXXXXXXXX"
android:textColor="#000"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="40dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_weight="1"
android:gravity="center"
>
<Button
android:id="@+id/register_dialog_ok_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#0066cc"
android:gravity="center"
android:text="确定"
android:textColor="#fff"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_weight="1"
android:gravity="center"
>
<Button
android:id="@+id/register_dialog_cancel_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#0066cc"
android:gravity="center"
android:text="取消"
android:textColor="#fff"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
2.Java代码演示样例:
(这里仅仅贴出和对话框有关的代码,即一个处理自己定义Dialog的方法)
//此方法用于处理自己定义对话框
private void processCustomDialog(AlertDialog.Builder builder){
LayoutInflater inflater=getLayoutInflater();
final View view=inflater.inflate(R.layout.register_dialog, null);//获取自己定义对话框资源
//获取自己定义dialog下的确定和取消button资源
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下的确定和取消button设置点击事件
*/
okBtnInDialog.setOnClickListener(new android.view.View.OnClickListener() {//以下为自己定义dialog下的确定button设置点击事件
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Register.this,Vertify.class);
startActivity(intent);
MyThread thread = new MyThread();
thread.start();
}
});
cancelBtnInDialog.setOnClickListener(new android.view.View.OnClickListener() {//以下为自己定义dialog下的取消button设置点击事件
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
}
值得注意的是:在获取Dialog上的button等资源时。使用的是view.findViewById(R.id.button)。这才表明你是在对话框View中寻找资源。倘若直接用findViewById(R.id.button),表明在此activity里找资源ID,能找到才怪!
相关文章
- 【VS开发】 自己编写一个简单的ActiveX控件——详尽教程
- Android 中常见控件的介绍和使用
- SharePoint 中用户控件的开发及应用
- 文件上传控件bootstrap-fileinput的使用
- ExtJs控件属性配置详细
- WEB网站类型系统中使用的OFFICE控件
- AB2D控件ZoomPanel Crack,WPF动画缩放和平移功能
- 零基础学鸿蒙编程-UI控件_PageSlider
- 利用安卓原生控件实现下拉刷新和上拉加载(SwipeRefreshLayout+Paging)
- ocx控件避免弹出警告的类--2
- 用ATL写简单的ActiveX控件 .
- 2018-8-10-控件
- [开源]在iOS上实现Android风格的控件Toast
- VC/MFC 使edit控件不能进行粘贴操作