android 中的 AlertDialog 对话框
由 AlertDialog.builder 进行创建,创建后使用 show 显示,使用简单,代码如下
对话框的 xml 定义
<?
xml version="1.0" encoding="utf-8"
?>
<
LinearLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:layout_width
="match_parent"
android:layout_height
="match_parent"
android:orientation
="vertical"
>
<
TextView
android:id
="@+id/username"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_marginLeft
="20dip"
android:layout_marginRight
="20dip"
android:text
="账号"
android:gravity
="left"
android:textAppearance
="?android:attr/textAppearanceMedium"
/>
<
EditText
android:id
="@+id/username"
android:layout_height
="wrap_content"
android:layout_width
="fill_parent"
android:layout_marginLeft
= "20dip"
android:layout_marginRight
="20dip"
android:scrollHorizontally
="true"
android:autoText
="false"
android:capitalize
="none"
android:gravity
="fill_horizontal"
android:textAppearance
="?android:attr/textAppearanceMedium"
/>
<
TextView
android:id
="@+id/password"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_marginLeft
="20dip"
android:layout_marginRight
="20dip"
android:text
="密码"
android:gravity
="left"
android:textAppearance
="?android:attr/textAppearanceMedium"
/>
<
EditText
android:id
="@+id/password"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:layout_marginLeft
="20dip"
android:layout_marginRight
="20dip"
android:scrollHorizontally
="true"
android:autoText
="false"
android:capitalize
="none"
android:gravity
="fill_horizontal"
android:password
="true"
/>
</
LinearLayout
>
java 代码
package zziss.android.dialogtest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
public
class DialogTestActivity
extends Activity {
/**
Called when the activity is first created.
*/
ProgressDialog m_dialog;
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
Builder builder = new AlertDialog.Builder(DialogTestActivity.this);
//
Dialog dialog = builder.create();
//
dialog.setTitle("登录提示");
/*
Dialog dialog = new AlertDialog.Builder(DialogTestActivity.this)
.setTitle("登录提示")
.setMessage("这里需要登录")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
LayoutInflater factory = LayoutInflater.from(DialogTestActivity.this);
final View DialogView = factory.inflate(R.layout.dialog, null);
AlertDialog dlg = new AlertDialog.Builder(DialogTestActivity.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(
DialogTestActivity.this,
"请等待...", "正在为您登录...",true);
new Thread()
{
public void run()
{
try
{
sleep(3000);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
m_dialog.dismiss();
}
}
}.start(); // end thread
} // end ok click
}
)
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
DialogTestActivity.this.finish();
}
}
).create();
dlg.show();
};
}
)
.setNeutralButton("退出",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
DialogTestActivity.this.finish();
}
}
).create();
dialog.show();
*/
initDialogClick();
AlertDialog.Builder builder =
new AlertDialog.Builder(DialogTestActivity.
this);
builder.setTitle("登录对话框");
builder.setMessage("这里需要登录")
.setPositiveButton("确定",dialogclick )
.setNegativeButton("取消", dialogclick);
Dialog dialog = builder.create();
dialog.show();
}
private DialogInterface.OnClickListener dialogclick;
private DialogInterface.OnClickListener dialogLoginClick;
private
void initDialogClick()
{
dialogclick =
new DialogInterface.OnClickListener() {
@Override
public
void onClick(DialogInterface dialog,
int which) {
//
TODO Auto-generated method stub
if (which == dialog.BUTTON_POSITIVE)
{
showLoginDialog();
}
if (which == dialog.BUTTON_NEGATIVE)
{
showMessage("点击了取消按纽");
}
}
};
dialogLoginClick =
new DialogInterface.OnClickListener() {
@Override
public
void onClick(DialogInterface dialog,
int which) {
//
TODO Auto-generated method stub
if (which == dialog.BUTTON_POSITIVE)
{
showProgressDialog();
}
if (which == dialog.BUTTON_NEGATIVE)
{
dialog.dismiss();
}
}
};
}
//
end initDialogClick
private
void showLoginDialog()
{
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View logindialog = inflater.inflate(R.layout.dialog,
null);
AlertDialog login =
new AlertDialog.Builder(
this)
.setTitle("请登录")
.setView(logindialog)
.setPositiveButton("登录", dialogLoginClick)
.setNegativeButton("取消", dialogLoginClick)
.create();
login.show();
}
private
void showProgressDialog()
{
m_dialog = ProgressDialog.show(
DialogTestActivity.
this,
"请等待...", "正在为您登录...",
true);
new Thread()
{
public
void run()
{
try
{
sleep(3000);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
m_dialog.dismiss();
}
}
}.start();
//
end thread
}
private
void showMessage(String str)
{
Toast toast = Toast.makeText(
this, str, Toast.LENGTH_SHORT);
toast.show();
}
}
上面的注释中的代码是书上的,不太好看懂,所以就拆开了
点击alertdialog之外其他屏幕,alertdialog消失/dismiss
很简单,设置一下alertDialog的属性就行:
AlertDialog alert = builder.create();
alert.setCanceledOnTouchOutside(true);//详细信息可以查看官方文档
alert.show();