getLayoutInflater().inflate

 

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

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


对话框中所显示的XML文件

  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>  

getLayoutinflate.inflate方法得到我们要显示的xml文件,再用builder.setView方法将取出来的xml文件装载进对话框中

  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. }

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值