当前位置: 首页 > news >正文

getLayoutInflater().inflate .

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

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

 

对话框中所显示的XML文件

 

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

相关文章:

  • 前端lvs+keepalived 后端 lnmp 群集 mysql主从+sersync
  • 做好一个高效的程序员吧
  • OSPF各种LSA的比较
  • 数据库热备容灾方案(上海某医院)
  • Cygwin sshd Apache sshd
  • 【046】◀▶ HTML 关键字
  • Eclipse is running in a JRE, but a JDK is required错误的解决
  • Memstat -- 查看Linux共享库的内存占用
  • Introduction-To-Signal-Processingorfanidis-i2sp
  • IBM AIX 系统常用命令
  • HUT-1674 火柴棒等式
  • [技巧] 如何查看Debian/Ubuntu软件包.deb的内容
  • PrestaShop支付接口-网银在线支付,在线支付收款,外贸收款
  • 嵌入式第一课:嵌入式系统概述
  • Thrift之TProtocol类体系原理及源码详细解析之JSon协议类TJSONProtocol
  • __proto__ 和 prototype的关系
  • 【附node操作实例】redis简明入门系列—字符串类型
  • HTTP中的ETag在移动客户端的应用
  • JWT究竟是什么呢?
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • vue学习系列(二)vue-cli
  • 爱情 北京女病人
  • 排序(1):冒泡排序
  • 前端面试总结(at, md)
  • 前端之React实战:创建跨平台的项目架构
  • 如何进阶一名有竞争力的程序员?
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 设计模式(12)迭代器模式(讲解+应用)
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • #《AI中文版》V3 第 1 章 概述
  • #我与Java虚拟机的故事#连载13:有这本书就够了
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (bean配置类的注解开发)学习Spring的第十三天
  • (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令...
  • (SpringBoot)第七章:SpringBoot日志文件
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (力扣记录)1448. 统计二叉树中好节点的数目
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (转) Face-Resources
  • (转载)Google Chrome调试JS
  • *Algs4-1.5.25随机网格的倍率测试-(未读懂题)
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .NET 跨平台图形库 SkiaSharp 基础应用
  • .NET 使用 XPath 来读写 XML 文件
  • .NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序)...
  • .net6解除文件上传限制。Multipart body length limit 16384 exceeded
  • .NET的微型Web框架 Nancy
  • .NET学习教程二——.net基础定义+VS常用设置
  • .NET应用架构设计:原则、模式与实践 目录预览
  • :not(:first-child)和:not(:last-child)的用法
  • ??eclipse的安装配置问题!??
  • @LoadBalanced 和 @RefreshScope 同时使用,负载均衡失效分析
  • @RequestMapping 的作用是什么?