AlertDialog添加控件 .

在项目开发中,经常用到AlertDialog提示用户信息,简单的Dialog提醒,或者警告信息都很esey的实现, 在dialog进行操作这种实现在开发中也很常用。 今天就做一个简单dialog添加控件的例子。言语不多,直接看例子。

dialog.xml源代码:

[html] view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3.     <!-- dialog layout -->  
  4.   
  5. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  6.   
  7.     android:orientation="vertical" android:layout_width="fill_parent"  
  8.   
  9.     android:layout_height="wrap_content">  
  10.   
  11.     <LinearLayout android:orientation="horizontal"  
  12.   
  13.         android:layout_width="fill_parent" android:layout_height="wrap_content"  
  14.   
  15.         android:padding="2dip" android:gravity="center">  
  16.   
  17.   
  18.   
  19.         <TextView android:width="60dip" android:text="工号:"  
  20.   
  21.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  22.   
  23.             android:typeface="monospace" android:textColor="#FFF" />  
  24.   
  25.   
  26.   
  27.         <EditText android:id="@+id/job_number" android:width="180dip"  
  28.   
  29.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  30.   
  31.             android:typeface="monospace" android:textColor="#FFF" />  
  32.   
  33.     </LinearLayout>  
  34.   
  35.   
  36.   
  37.     <LinearLayout android:orientation="horizontal"  
  38.   
  39.         android:layout_width="fill_parent" android:layout_height="wrap_content"  
  40.   
  41.         android:padding="2dip" android:gravity="center">  
  42.   
  43.   
  44.   
  45.         <TextView android:width="60dip" android:text="口令:"  
  46.   
  47.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  48.   
  49.             android:typeface="monospace" android:textColor="#FFF" />  
  50.   
  51.   
  52.   
  53.         <EditText android:id="@+id/shibboleth" android:width="180dip"  
  54.   
  55.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  56.   
  57.             android:typeface="monospace" android:textColor="#FFF" />  
  58.   
  59.     </LinearLayout>  
  60.   
  61. </LinearLayout>  
DialogActivity.java源代码:

[java] view plain copy print ?
  1. package com.wanghf.demo;  
  2.   
  3. import android.app.Activity;  
  4.   
  5. import android.app.AlertDialog;  
  6.   
  7. import android.app.ProgressDialog;  
  8.   
  9. import android.content.DialogInterface;  
  10.   
  11. import android.os.Bundle;  
  12.   
  13. import android.view.LayoutInflater;  
  14.   
  15. import android.view.View;  
  16.   
  17. /** 
  18.  *  
  19.  * AlertDialog 添加控件DEMO 
  20.  *  
  21.  * @author android_home 
  22.  *  
  23.  * @time 2011-07-20 11:05:03 
  24.  */  
  25.   
  26. public class DialogActivity extends Activity {  
  27.   
  28.     ProgressDialog m_Dialog;  
  29.   
  30.     /** Called when the activity is first created. */  
  31.   
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.   
  35.         super.onCreate(savedInstanceState);  
  36.   
  37.         setContentView(R.layout.main);  
  38.   
  39.         LayoutInflater inflater = LayoutInflater  
  40.   
  41.         .from(DialogActivity.this);  
  42.   
  43.         // 得到自定义对话框   
  44.   
  45.         final View DialogView = inflater  
  46.   
  47.         .inflate(R.layout.dialog, null);  
  48.   
  49.         // 创建对话框   
  50.   
  51.         AlertDialog alertDialog = new AlertDialog.Builder(DialogActivity.this)  
  52.   
  53.         .setTitle("目标确认")  
  54.   
  55.         .setView(DialogView) // 设置自定义对话框样式   
  56.   
  57.                 .setPositiveButton("确定",  
  58.   
  59.                 new DialogInterface.OnClickListener() {  
  60.                     @Override  
  61.                     public void onClick(DialogInterface dialog, int which) {  
  62.   
  63.                         // TODO Auto-generated method stub   
  64.   
  65.                         // 输入完成,点击确定登录   
  66.   
  67.                         m_Dialog = ProgressDialog.show(DialogActivity.this,  
  68.   
  69.                         "请等待...""系统正在登录..."true);  
  70.   
  71.                         new Thread()  
  72.   
  73.                         {  
  74.                             public void run() {  
  75.   
  76.                                 try {  
  77.                                     sleep(3000);  
  78.                                 } catch (Exception e) {  
  79.   
  80.                                     // TODO: handle exception   
  81.   
  82.                                     e.printStackTrace();  
  83.   
  84.                                 } finally {  
  85.   
  86.                                     // 登录结束,取消m_Dialog对话框   
  87.   
  88.                                     m_Dialog.dismiss();  
  89.                                 }  
  90.   
  91.                             }  
  92.   
  93.                         }.start();  
  94.   
  95.                     }  
  96.   
  97.                 }).setNegativeButton("取消",  
  98.   
  99.                 new DialogInterface.OnClickListener() {  
  100.                     @Override  
  101.                     public void onClick(DialogInterface dialog, int which) {  
  102.                         // 点击取消后推出Activity01   
  103.   
  104.                         DialogActivity.this.finish();  
  105.   
  106.                     }  
  107.   
  108.                 }).create();// 创建   
  109.   
  110.         alertDialog.show();  
  111.   
  112.     }  
  113.   
  114. }  

显示结果。。如图:

AlertDialog添加控件 ._第1张图片


你可能感兴趣的:(AlertDialog添加控件 .)