getLayoutInflater().inflate
16lz
2021-01-25
之前我们使用SimpleAdapter创建了一个自定义的对话框,android还允许我们在对话框中显示指定的xml文件,从而实现自定义对话框的效果。
单击按钮,弹出自定义的登录对话框。
对话框中所显示的XML文件
[html] view plain copy print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/loginForm"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TableRow>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="用户名:"
- android:textSize="10pt"/>
- <!--输入用户名的文本框-->
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="请填写登录帐号"
- android:selectAllOnFocus="true"/>
- </TableRow>
- <TableRow>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="密码:"
- android:textSize="10pt"/>
- <!--输入密码的文本框-->
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:password="true"/>
- </TableRow>
- <TableRow>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="电话号码:"
- android:textSize="10pt"/>
- <!--输入电话号码的文本框-->
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="请填写您的电话号码"
- android:phoneNumber="true"
- android:selectAllOnFocus="true"/>
- </TableRow>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="注册"/>
- </TableLayout>
用getLayoutinflate.inflate方法得到我们要显示的xml文件,再用builder.setView方法将取出来的xml文件装载进对话框中
[java] view plain copy print ?- publicclassLoginDialogextendsActivity
- {
- @Override
- publicvoidonCreate(BundlesavedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Buttonbn=(Button)findViewById(R.id.bn);
- //定义一个AlertDialog.Builder对象
- finalBuilderbuilder=newAlertDialog.Builder(this);
- //为按钮绑定事件监听器
- bn.setOnClickListener(newView.OnClickListener()
- {
- @Override
- publicvoidonClick(Viewsource)
- {
- //设置对话框的图标
- builder.setIcon(R.drawable.tools);
- //设置对话框的标题
- builder.setTitle("自定义普通对话框");
- //装载/res/layout/login.xml界面布局
- TableLayoutloginForm=(TableLayout)getLayoutInflater()
- .inflate(R.layout.login,null);
- //设置对话框显示的View对象
- builder.setView(loginForm);
- //为对话框设置一个“确定”按钮
- builder.setPositiveButton("登录"
- //为按钮设置监听器
- ,newOnClickListener()
- {
- @Override
- publicvoidonClick(DialogInterfacedialog,intwhich)
- {
- //此处可执行登录处理
- }
- });
- //为对话框设置一个“取消”按钮
- builder.setNegativeButton("取消"
- ,newOnClickListener()
- {
- @Override
- publicvoidonClick(DialogInterfacedialog,intwhich)
- {
- //取消登录,不做任何事情。
- }
- });
- //创建、并显示对话框
- builder.create().show();
- }
- });
- }
- }
更多相关文章
- 一步步教你实现Android(安卓)HotFix热更新
- Android得不到对话框中EditView值的问题
- 整理 酷炫 Android(安卓)开源UI框架 FAB
- android ui ->>Dialog对话框
- android 实现录像时拍照
- android的Dialog使用
- 获取联系人信息和删除联系人
- 点击按钮之后仍然显示对话框--Android
- Android(安卓)简单游戏实现笑脸的移动与人物移动