一、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



http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

android:id="@+id/teBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content">





Step 2: text_entry_dialog.xml



http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

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" />


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" />

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" />


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" />


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();
}
});
}
}

更多相关文章

  1. android EditText设置不可写
  2. android“设置”里的版本号
  3. 在Fragment中设置控件点击方法,执行失败。
  4. Android(安卓)闹钟管理类的使用
  5. Android设置通知栏/状态栏透明改变通知栏颜色和app最上部分颜色
  6. android 设置中划线 下划线等
  7. Andorid Dialog 示例【慢慢更新】
  8. Android(安卓)P SystemUI之StatusBar UI布局status_bar.xml解析
  9. android图表ichartjs

随机推荐

  1. android 数据库操作 插入彩信,数据库子查
  2. android rgb颜色设置方法及常用颜色的RGB
  3. [Android]使用AdapterTypeRender对不同类
  4. 安卓基础(十)
  5. 如何在xml中直接添加view点击监听(参考ra
  6. java ios Android鸿鹄社交娱乐直播平台源
  7. Android(安卓)XMl文件存储及其解析
  8. Android的log里这代表什么:Activity idle
  9. 安卓UI四种基本布局-线性布局
  10. 如何在项目中封装 Kotlin + Android(安卓