之前我们使用SimpleAdapter创建了一个自定义的对话框,android还允许我们在对话框中显示指定的xml文件,从而实现自定义对话框的效果。

单击按钮,弹出自定义的登录对话框。


对话框中所显示的XML文件

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/loginForm"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical">
  7. <TableRow>
  8. <TextView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="用户名:"
  12. android:textSize="10pt"/>
  13. <!--输入用户名的文本框-->
  14. <EditText
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:hint="请填写登录帐号"
  18. android:selectAllOnFocus="true"/>
  19. </TableRow>
  20. <TableRow>
  21. <TextView
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:text="密码:"
  25. android:textSize="10pt"/>
  26. <!--输入密码的文本框-->
  27. <EditText
  28. android:layout_width="fill_parent"
  29. android:layout_height="wrap_content"
  30. android:password="true"/>
  31. </TableRow>
  32. <TableRow>
  33. <TextView
  34. android:layout_width="fill_parent"
  35. android:layout_height="wrap_content"
  36. android:text="电话号码:"
  37. android:textSize="10pt"/>
  38. <!--输入电话号码的文本框-->
  39. <EditText
  40. android:layout_width="fill_parent"
  41. android:layout_height="wrap_content"
  42. android:hint="请填写您的电话号码"
  43. android:phoneNumber="true"
  44. android:selectAllOnFocus="true"/>
  45. </TableRow>
  46. <Button
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:text="注册"/>
  50. </TableLayout>
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns: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 ?
  1. publicclassLoginDialogextendsActivity
  2. {
  3. @Override
  4. publicvoidonCreate(BundlesavedInstanceState)
  5. {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.main);
  8. Buttonbn=(Button)findViewById(R.id.bn);
  9. //定义一个AlertDialog.Builder对象
  10. finalBuilderbuilder=newAlertDialog.Builder(this);
  11. //为按钮绑定事件监听器
  12. bn.setOnClickListener(newView.OnClickListener()
  13. {
  14. @Override
  15. publicvoidonClick(Viewsource)
  16. {
  17. //设置对话框的图标
  18. builder.setIcon(R.drawable.tools);
  19. //设置对话框的标题
  20. builder.setTitle("自定义普通对话框");
  21. //装载/res/layout/login.xml界面布局
  22. TableLayoutloginForm=(TableLayout)getLayoutInflater()
  23. .inflate(R.layout.login,null);
  24. //设置对话框显示的View对象
  25. builder.setView(loginForm);
  26. //为对话框设置一个“确定”按钮
  27. builder.setPositiveButton("登录"
  28. //为按钮设置监听器
  29. ,newOnClickListener()
  30. {
  31. @Override
  32. publicvoidonClick(DialogInterfacedialog,intwhich)
  33. {
  34. //此处可执行登录处理
  35. }
  36. });
  37. //为对话框设置一个“取消”按钮
  38. builder.setNegativeButton("取消"
  39. ,newOnClickListener()
  40. {
  41. @Override
  42. publicvoidonClick(DialogInterfacedialog,intwhich)
  43. {
  44. //取消登录,不做任何事情。
  45. }
  46. });
  47. //创建、并显示对话框
  48. builder.create().show();
  49. }
  50. });
  51. }
  52. }

更多相关文章

  1. 一步步教你实现Android(安卓)HotFix热更新
  2. Android得不到对话框中EditView值的问题
  3. 整理 酷炫 Android(安卓)开源UI框架 FAB
  4. android ui ->>Dialog对话框
  5. android 实现录像时拍照
  6. android的Dialog使用
  7. 获取联系人信息和删除联系人
  8. 点击按钮之后仍然显示对话框--Android
  9. Android(安卓)简单游戏实现笑脸的移动与人物移动

随机推荐

  1. Win 8或以上系统下MySQL最新版5.7.17(64bi
  2. MySQL 5.7.18 免安装版配置教程
  3. Linux下MySQL 5.5/5.6的修改字符集编码为
  4. mysql5.7.18解压版启动mysql服务
  5. MySql在Mac上的安装与配置详解
  6. mysql允许所有host访问的方法
  7. MySQL分页分析原理及提高效率
  8. 解决mysql创建数据库后出现:Access denied
  9. 解决MySQL 5.7.9版本sql_mode=only_full_
  10. mysql5.7.18.zip免安装版本配置教程(windo