AlertDialog添加控件 .

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

dialog.xml源代码:

  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>  
<?xml version="1.0" encoding="utf-8"?>

	<!-- dialog layout -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

	android:orientation="vertical" android:layout_width="fill_parent"

	android:layout_height="wrap_content">

	<LinearLayout android:orientation="horizontal"

		android:layout_width="fill_parent" android:layout_height="wrap_content"

		android:padding="2dip" android:gravity="center">



		<TextView android:width="60dip" android:text="工号:"

			android:layout_width="wrap_content" android:layout_height="wrap_content"

			android:typeface="monospace" android:textColor="#FFF" />



		<EditText android:id="@+id/job_number" android:width="180dip"

			android:layout_width="wrap_content" android:layout_height="wrap_content"

			android:typeface="monospace" android:textColor="#FFF" />

	</LinearLayout>



	<LinearLayout android:orientation="horizontal"

		android:layout_width="fill_parent" android:layout_height="wrap_content"

		android:padding="2dip" android:gravity="center">



		<TextView android:width="60dip" android:text="口令:"

			android:layout_width="wrap_content" android:layout_height="wrap_content"

			android:typeface="monospace" android:textColor="#FFF" />



		<EditText android:id="@+id/shibboleth" android:width="180dip"

			android:layout_width="wrap_content" android:layout_height="wrap_content"

			android:typeface="monospace" android:textColor="#FFF" />

	</LinearLayout>

</LinearLayout>
DialogActivity.java源代码:

  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. }  
package com.wanghf.demo;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.ProgressDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

/**
 * 
 * AlertDialog 添加控件DEMO
 * 
 * @author android_home
 * 
 * @time 2011-07-20 11:05:03
 */

public class DialogActivity extends Activity {

	ProgressDialog m_Dialog;

	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		LayoutInflater inflater = LayoutInflater

		.from(DialogActivity.this);

		// 得到自定义对话框

		final View DialogView = inflater

		.inflate(R.layout.dialog, null);

		// 创建对话框

		AlertDialog alertDialog = new AlertDialog.Builder(DialogActivity.this)

		.setTitle("目标确认")

		.setView(DialogView) // 设置自定义对话框样式

				.setPositiveButton("确定",

				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

						// TODO Auto-generated method stub

						// 输入完成,点击确定登录

						m_Dialog = ProgressDialog.show(DialogActivity.this,

						"请等待...", "系统正在登录...", true);

						new Thread()

						{
							public void run() {

								try {
									sleep(3000);
								} catch (Exception e) {

									// TODO: handle exception

									e.printStackTrace();

								} finally {

									// 登录结束,取消m_Dialog对话框

									m_Dialog.dismiss();
								}

							}

						}.start();

					}

				}).setNegativeButton("取消",

				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 点击取消后推出Activity01

						DialogActivity.this.finish();

					}

				}).create();// 创建

		alertDialog.show();

	}

}

显示结果。。如图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值