本文围绕以下两个部分展开:
一、一个案例
1、主界面
2、对话框
3、列表对话框
4、单选对话框
5、多选对话框
6、自定义对话框
附 代码补充
一、一个案例
1、主界面
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/btnAlertDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="@string/btn_alert_dialog"/>
<Button
android:id="@+id/btnListAlertDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnAlertDialog"
android:onClick="onClick"
android:text="@string/btn_list_alert_dialog"/>
<Button
android:id="@+id/btnSingleListAlertDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnListAlertDialog"
android:onClick="onClick"
android:text="@string/btn_single_list_alert_dialog"/>
<Button
android:id="@+id/btnMultiListAlertDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnSingleListAlertDialog"
android:onClick="onClick"
android:text="@string/btn_multi_list_alert_dialog"/>
<Button
android:id="@+id/btnCustomDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnMultiListAlertDialog"
android:onClick="onClick"
android:text="@string/btn_custom_dialog"/>
</RelativeLayout>
2、对话框
private void alertDialog() {
// Builder 模式:建造者模式
// AlertDialog 的构造方法【受保护】,使用 Builder 创建对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 设置对话框的图标 / 标题 / 内容
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle(getString(R.string.app_name));
builder.setMessage("夜深了~~~");
// 不可取消【默认 true】
builder.setCancelable(false);
// 设置按钮:确定、取消、中立【显示的位置固定】
builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("OK Button.");
}
});
builder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Cancel Button.");
}
});
builder.setNeutralButton(R.string.button_neutral, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Neutral Button.");
}
});
// 创建对话框
AlertDialog dialog = builder.create();
dialog.show();
}
3、列表对话框
private void listAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 设置对话框的图标 / 标题
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle(getString(R.string.btn_list_alert_dialog));
// 参数一:数据
// 参数二:选项的事件
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击哪个选项就显示此选项 (which - 字符串数组的下标)
showToast(items[which]);
}
});
// 设置 取消 按钮,无监听事件
builder.setNegativeButton(R.string.button_cancel, null);
AlertDialog dialog = builder.create();
dialog.show();
}
4、单选对话框
private void singleListAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle(getString(R.string.btn_single_list_alert_dialog));
// 参数一:列表中的数据集
// 参数二:默认选中的位置,-1 无默认选中项
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击哪个选项就显示此选项 (which - 字符串数组的下标)
showToast(items[which]);
}
});
builder.setNegativeButton(R.string.button_cancel, null);
builder.setPositiveButton(R.string.button_ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
5、多选对话框
private void multiListAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle(getString(R.string.btn_multi_list_alert_dialog));
builder.setCancelable(false);
final boolean[] checkedItems = new boolean[items.length];
// 参数一:列表中的数据集
// 参数二:列表中各项的状态(true 为选中、false 为未选中)
// 参数三:多选监听器
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
}
});
builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String text = "";
for (int i = 0; i < items.length; i++) {
if (checkedItems[i]) {
text += items[i] + ",";
}
}
if (text.length() > 0) {
text = text.substring(0, text.length() - 1);
} else {
text = "你还没有选呢~";
}
showToast(text + "\n" + Arrays.toString(checkedItems));
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
6、自定义对话框
(1)dialog_custom.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"
android:padding="@dimen/activity_horizontal_margin">
<EditText
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_name"/>
<EditText
android:id="@+id/txtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_password"
android:inputType="textPassword"/>
</LinearLayout>
(2)
private void customDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle(getString(R.string.btn_custom_dialog));
// 使用指定布局文件填充视图
View view = this.getLayoutInflater().inflate(
R.layout.dialog_custom, null);
txtName = (EditText) view.findViewById(R.id.txtName);
txtPassword = (EditText) view.findViewById(R.id.txtPassword);
// 设置对话框的视图
builder.setView(view);
builder.setNegativeButton(R.string.button_cancel, null);
builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = txtName.getText().toString();
String password = txtPassword.getText().toString();
showToast(name + "\n" + password);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
附 代码补充
1. styles.xml(v21) :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="android:Theme.Material.Light">
<item name="android:colorPrimaryDark">@android:color/holo_blue_dark</item>
<item name="android:colorPrimary">@android:color/holo_blue_light</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
</style>
</resources>
2. strings.xml :
<resources>
<string name="app_name">Dialog</string>
<string name="action_settings">Settings</string>
<string name="btn_alert_dialog">对话框</string>
<string name="btn_list_alert_dialog">列表对话框</string>
<string name="btn_single_list_alert_dialog">单选列表对话框</string>
<string name="btn_multi_list_alert_dialog">多选列表对话框</string>
<string name="btn_custom_dialog">自定义对话框</string>
<string name="button_ok">确定</string>
<string name="button_cancel">取消</string>
<string name="button_neutral">了解更多</string>
<string name="hint_name">网络名</string>
<string name="hint_password">密码</string>
<string-array name="skills">
<item>Android</item>
<item>iOS</item>
<item>Windows</item>
<item>Web</item>
<item>C/C++</item>
</string-array>
</resources>
3. MainActivity :
package com.android.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Arrays;
public class MainActivity extends Activity {
private String[] items;
private EditText txtName;
private EditText txtPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 通过读取资源文件 strings.xml 获得指定的字符串数组
items = this.getResources().getStringArray(R.array.skills);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnAlertDialog:
alertDialog();
break;
case R.id.btnListAlertDialog:
listAlertDialog();
break;
case R.id.btnSingleListAlertDialog:
singleListAlertDialog();
break;
case R.id.btnMultiListAlertDialog:
multiListAlertDialog();
break;
case R.id.btnCustomDialog:
customDialog();
break;
}
}
private void customDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle(getString(R.string.btn_custom_dialog));
// 使用指定布局文件填充视图
View view = this.getLayoutInflater().inflate(
R.layout.dialog_custom, null);
txtName = (EditText) view.findViewById(R.id.txtName);
txtPassword = (EditText) view.findViewById(R.id.txtPassword);
// 设置对话框的视图
builder.setView(view);
builder.setNegativeButton(R.string.button_cancel, null);
builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = txtName.getText().toString();
String password = txtPassword.getText().toString();
showToast(name + "\n" + password);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private void multiListAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle(getString(R.string.btn_multi_list_alert_dialog));
builder.setCancelable(false);
final boolean[] checkedItems = new boolean[items.length];
// 参数一:列表中的数据集
// 参数二:列表中各项的状态(true 为选中、false 为未选中)
// 参数三:多选监听器
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
}
});
builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String text = "";
for (int i = 0; i < items.length; i++) {
if (checkedItems[i]) {
text += items[i] + ",";
}
}
if (text.length() > 0) {
text = text.substring(0, text.length() - 1);
} else {
text = "你还没有选呢~";
}
showToast(text + "\n" + Arrays.toString(checkedItems));
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private void singleListAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle(getString(R.string.btn_single_list_alert_dialog));
// 参数一:列表中的数据集
// 参数二:默认选中的位置,-1 无默认选中项
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击哪个选项就显示此选项 (which - 字符串数组的下标)
showToast(items[which]);
}
});
builder.setNegativeButton(R.string.button_cancel, null);
builder.setPositiveButton(R.string.button_ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
private void listAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 设置对话框的图标 / 标题 / 内容
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle(getString(R.string.btn_list_alert_dialog));
// 参数一:数据
// 参数二:选项的事件
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击哪个选项就显示此选项 (which - 字符串数组的下标)
showToast(items[which]);
}
});
// 设置 取消 按钮,无监听事件
builder.setNegativeButton(R.string.button_cancel, null);
AlertDialog dialog = builder.create();
dialog.show();
}
private void alertDialog() {
// Builder 模式:建造者模式
// AlertDialog 的构造方法【受保护】,使用 Builder 创建对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 设置对话框的图标 / 标题 / 内容
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle(getString(R.string.app_name));
builder.setMessage("夜深了~~~");
// 不可取消【默认 true】
builder.setCancelable(false);
// 设置按钮:确定、取消、中立【显示的位置固定】
builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("OK Button.");
}
});
builder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Cancel Button.");
}
});
builder.setNeutralButton(R.string.button_neutral, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showToast("Neutral Button.");
}
});
// 创建对话框
AlertDialog dialog = builder.create();
dialog.show();
}
private void showToast(String text) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}