一、AlertDialog.Builder

Android 中的alertDialog 的创建一般是通过其内嵌类AlertDialog.Builder 来实现的。所以首先浏览一下这个builder 所提供的方法: setTitle() :给对话框设置 title. setIcon(): 给对话框设置图标。 setMessage(): 设置对话框的提示信息 setItems() :设置对话框要显示的一个 list, 一般用于要显示几个命令时 setSingleChoiceItems(): 设置对话框显示一个单选的 List setMultiChoiceItems(): 用来设置对话框显示一系列的复选框。 setPositiveButton(): 给对话框添加 ”Yes” 按钮。 setNegativeButton(): 给对话框添加 ”No” 按钮。

二、常见对话框:

在了解完这几个常用的方法之后,看一个小例子,创建一个用来提示的对话框:
Dialog dialog = new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(“title”)
.setMessage(“这里是提示信息语句”)
.setPositiveButton(“Ok”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

/* User clicked OK so do some stuff */
}
})
.setNeutralButton(“Cancel”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

/* User clicked Something so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

/* User clicked Cancel so do some stuff */
}
})
.create();
dialog.show();//如果要显示对话框,一定要加上这句


另外在我的以前的一篇博客中的代码中介绍了如何创建一个包含single choice 或者command list 的对话框,具体请参考这里: http://blog.chinaunix.net/u/20947/showart_1962223.html

三、包含定制view的对话框:

很多时候,我们需要在对话框中显示一个特定的view, 比如说用户登录对话框,就需要显示要用户输入用户名和密码的editBox 等。 要想让对话框显示一个view, 我们就要用到AlertDialog.Builder setView(View view) 方法来,可以看到该方法的参数是一个view, 所以我们就需要先取得这个view, 这个时候我们还要用到一个新的class, 那就是LayoutFlater ,它的作用就是将一个layout xml 文件转化成一个view 实例。 最后,还是用一个实例来演示一下如何在对话框中将定制的view 作为其内容,该程序用来演示如何通过点击一个按钮来调用一个用来显示登陆的对话框界面,如图: Step 1: main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<Button android:text="User log in..."
android:id="@+id/teBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>

Step 2: text_entry_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/username_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="Username"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
android:id="@+id/username_edit"
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_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="Password"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
android:id="@+id/password_edit"
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:password="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

Step 3: code

package com.zx.te;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class TextEntryDialogTest extends Activity {
Button btnTEDlg;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btnTEDlg = (Button)findViewById(R.id.teBtn);
btnTEDlg.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
LayoutInflater factory = LayoutInflater.from(TextEntryDialogTest.this);
final View textEntryView = factory.inflate(R.layout.text_entry_dialog, null);
AlertDialog dlg = new AlertDialog.Builder(TextEntryDialogTest.this)
.setTitle("User Login")
.setView(textEntryView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

/* User clicked OK so do some stuff */
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

/* User clicked cancel so do some stuff */
}
})
.create();
dlg.show();
}
});
}
}
发表于: 2009-06-15,修改于: 2009-06-15 15:02

更多相关文章

  1. 为什么用Toast,而不是AlertDialog
  2. 让Android模拟器上网
  3. ADT/AndroidSDK 模拟器 安装与卸载APK
  4. android之DecorView
  5. Unity与AndroidStudio对接后,Unity打包Apk报错:CommandInvokationF
  6. Android(安卓)定时让popupwindow消失
  7. 【Android】悬浮按钮(FloatingActionButton)
  8. Android(安卓)系统api实现定位及使用百度提供的api来实现定位
  9. 2012传智播客黑马程序员内部视频

随机推荐

  1. android之视频播放
  2. 《Android经验分享》周刊第10期
  3. 学习Android到底能赚多少钱?
  4. Android: Binder
  5. Android(安卓)分区挂载
  6. Android跨进程通信-AIDL
  7. Android(安卓)AsyncTask完全解析,带你从源
  8. Android界面布局
  9. [Android] [ANR的原理、分析、实战] 【转
  10. Android之网络请求1————HTTP协议