在项目开发中,经常用到AlertDialog提示用户信息,简单的Dialog提醒,或者警告信息都很esey的实现, 在dialog进行操作这种实现在开发中也很常用。 今天就做一个简单dialog添加控件的例子。言语不多,直接看例子。
dialog.xml源代码:
- <?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>
<?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源代码:
- 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();
- }
- }
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();
}
}
显示结果。。如图: