场景
app中常见的对话框。
简单的带确定取消按钮的对话框
带列表的对话框
带单项选择的对话框
带多项选择的对话框
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
将布局改为LinearLayout,并通过android:orientation="vertical">设置为垂直布局。并添加四个按钮
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".AlertDialogActivity"> <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#090808"
android:background="#F44336"
android:text="" /> <Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#2196F3"
android:background="#FF9800"
android:text="" /> <Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#F44336"
android:background="#43b243"
android:text="" /> <Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#9C27B0"
android:background="#f9671e"
android:text="" /> </LinearLayout>
然后来到Activity
带确定取消按钮的对话框
// 获取“显示带取消、确定按钮的对话框”按钮
Button button1 = (Button) findViewById(R.id.button1);
// 为“显示带取消、确定按钮的对话框”按钮添加单击事件监听器
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建对话框对象 这里要通过Builder来创建
AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
alertDialog.setIcon(R.drawable.dog); //设置对话框的图标
alertDialog.setTitle("公众号:"); //设置对话框的标题
alertDialog.setMessage("霸道的程序猿"); //设置要显示的内容
//sdk版本问题
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
//添加取消按钮
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this, "您单击了否按钮", Toast.LENGTH_SHORT).show();
}
});
}
//添加确定按钮
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this, "您单击了是按钮 ", Toast.LENGTH_SHORT).show();
}
});
}
alertDialog.show(); //显示对话框
}
});
带列表的对话框
// 获取“显示带列表的对话框”按钮
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建水果字符串数组
final String[] items = new String[]{"苹果", "橘子", "香蕉",
"西瓜"};
//创建列表对话框对象
AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);
//设置对话框的图标
builder.setIcon(R.drawable.dog);
//设置对话框的标题
builder.setTitle("请选择你喜欢的水果:");
//添加列表项
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this,
"您选择了" + items[which], Toast.LENGTH_SHORT).show();
}
});
builder.create().show(); // 创建对话框并显示
}
});
带单项选择的对话框
// 获取“显示带单选列表项的对话框”按钮
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建动物字符串数组
final String[] items = new String[]{"小猫", "小狗", "乌龟", "金鱼", "小猪"};
// 显示带单选列表项的对话框
AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);
builder.setIcon(R.drawable.dog); //设置对话框的图标
builder.setTitle("如果让你选择,你最喜欢哪一个:"); //设置对话框的标题
builder.setSingleChoiceItems(items, , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this,
"您选择了" + items[which], Toast.LENGTH_SHORT).show(); //显示选择结果
}
});
//添加确定按钮
builder.setPositiveButton("确定", null);
builder.create().show(); // 创建对话框并显示
}
});
带多项选择的对话框
首先声明两个变量用来存储各列表项要显示的内容和记录各列表项的状态
private boolean[] checkedItems;//记录各列表项的状态
private String[] items;//各列表项要显示的内容
然后
// 获取“显示带多选列表项的对话框”按钮
Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkedItems = new boolean[]{false, true, false, true, false}; //记录各列表项的状态
//各列表项要显示的内容
items = new String[]{"开心消消乐", "球球大作战", "欢乐斗地主", "梦幻西游", "超级玛丽"};
// 显示带单选列表项的对话框
AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);
builder.setIcon(R.drawable.dog); //设置对话框的图标
builder.setTitle("请选择您喜爱的游戏:"); //设置对话框标题
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked; //改变被操作列表项的状态 }
});
//为对话框添加“确定”按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String result = "";
for (int i = ; i < checkedItems.length; i++) {
//当选项被选择时
if (checkedItems[i]) {
//将选项的内容添加到result中
result += items[i] + "、";
}
}
//当result不为空时,通过消息提示框显示选择的结果
if (!"".equals(result)) {
//去掉最后面添加的“、”号
result = result.substring(, result.length() - );
Toast.makeText(AlertDialogActivity.this,
"您选择了[ " + result + " ]", Toast.LENGTH_LONG).show();
} }
});
builder.create().show(); // 创建对话框并显示
}
});
完整示例代码
package com.badao.relativelayouttest; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class AlertDialogActivity extends AppCompatActivity { private boolean[] checkedItems;//记录各列表项的状态
private String[] items;//各列表项要显示的内容 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog);
// 获取“显示带取消、确定按钮的对话框”按钮
Button button1 = (Button) findViewById(R.id.button1);
// 为“显示带取消、确定按钮的对话框”按钮添加单击事件监听器
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建对话框对象 这里要通过Builder来创建
AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
alertDialog.setIcon(R.drawable.dog); //设置对话框的图标
alertDialog.setTitle("公众号:"); //设置对话框的标题
alertDialog.setMessage("霸道的程序猿"); //设置要显示的内容
//sdk版本问题
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
//添加取消按钮
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this, "您单击了否按钮", Toast.LENGTH_SHORT).show();
}
});
}
//添加确定按钮
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this, "您单击了是按钮 ", Toast.LENGTH_SHORT).show();
}
});
}
alertDialog.show(); //显示对话框
}
}); // 获取“显示带列表的对话框”按钮
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建水果字符串数组
final String[] items = new String[]{"苹果", "橘子", "香蕉",
"西瓜"};
//创建列表对话框对象
AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);
//设置对话框的图标
builder.setIcon(R.drawable.dog);
//设置对话框的标题
builder.setTitle("请选择你喜欢的水果:");
//添加列表项
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this,
"您选择了" + items[which], Toast.LENGTH_SHORT).show();
}
});
builder.create().show(); // 创建对话框并显示
}
}); // 获取“显示带单选列表项的对话框”按钮
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建动物字符串数组
final String[] items = new String[]{"小猫", "小狗", "乌龟", "金鱼", "小猪"};
// 显示带单选列表项的对话框
AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);
builder.setIcon(R.drawable.dog); //设置对话框的图标
builder.setTitle("如果让你选择,你最喜欢哪一个:"); //设置对话框的标题
builder.setSingleChoiceItems(items, , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogActivity.this,
"您选择了" + items[which], Toast.LENGTH_SHORT).show(); //显示选择结果
}
});
//添加确定按钮
builder.setPositiveButton("确定", null);
builder.create().show(); // 创建对话框并显示
}
}); // 获取“显示带多选列表项的对话框”按钮
Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkedItems = new boolean[]{false, true, false, true, false}; //记录各列表项的状态
//各列表项要显示的内容
items = new String[]{"开心消消乐", "球球大作战", "欢乐斗地主", "梦幻西游", "超级玛丽"};
// 显示带单选列表项的对话框
AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);
builder.setIcon(R.drawable.dog); //设置对话框的图标
builder.setTitle("请选择您喜爱的游戏:"); //设置对话框标题
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked; //改变被操作列表项的状态 }
});
//为对话框添加“确定”按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String result = "";
for (int i = ; i < checkedItems.length; i++) {
//当选项被选择时
if (checkedItems[i]) {
//将选项的内容添加到result中
result += items[i] + "、";
}
}
//当result不为空时,通过消息提示框显示选择的结果
if (!"".equals(result)) {
//去掉最后面添加的“、”号
result = result.substring(, result.length() - );
Toast.makeText(AlertDialogActivity.this,
"您选择了[ " + result + " ]", Toast.LENGTH_LONG).show();
} }
});
builder.create().show(); // 创建对话框并显示
}
});
}
}