示例图:
package com.example.myalertdialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
// AlertDialog对话框 应用
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//组件
private Button btnOne,btnTwo,btnThree,btnFour,btnFive;
private TextView textView;
private Context context;
//对话框
private AlertDialog.Builder builder =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context =this;
btnOne = findViewById(R.id.btn_one);
btnTwo = findViewById(R.id.btn_Two);
btnThree = findViewById(R.id.btn_three);
btnFour =findViewById(R.id.btn_four);
btnFive = findViewById(R.id.btn_five);
textView = findViewById(R.id.btn_tv);
//设置事件
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
btnThree.setOnClickListener(this);
btnFour.setOnClickListener(this);
btnFive.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn_one){
//普通对话框
builder = new AlertDialog.Builder(context);
//标题
builder.setTitle("游戏提示:");
//logo
builder.setIcon(R.mipmap.a);
//内容
builder.setMessage("您确认要开始游戏吗?");
//确定按钮
builder.setPositiveButton("开始", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context ,"开始游戏",Toast.LENGTH_SHORT).show();
}
});
//取消按钮 null 表示什么都不做
builder.setNegativeButton("取消",null);
//中立按钮
builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context,"中立您选择了"+which,Toast.LENGTH_SHORT).show();
}
});
//创建 显示
builder.create().show();
}else if (R.id.btn_Two == v.getId()) {
//单选对话框 1 只能选一次
//数据源
final String[] arr = new String[]{"校长","老师","学生","家长"};
//单选对话框
builder = new AlertDialog.Builder(context);
//标题
builder.setTitle("请选择您的身份:");
//图标
builder.setIcon(R.mipmap.a);
//内容
builder.setItems(arr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context,"您选择了"+arr[which],Toast.LENGTH_SHORT).show();
textView.setText(arr[which]);
}
});
//取消按钮
builder.setNegativeButton("取消",null);
//创建 显示
builder.create().show();
} else if (v.getId() == R.id.btn_three) {
// 单选对话框2 可反复选择
builder = new AlertDialog.Builder(context);
//数据源 2
final String[] arr = new String[]{"java","php","android","c++"};
//标题
builder.setTitle("请选择课程:");
//图标
builder.setIcon(R.mipmap.a);
//内容 默认选择项 -1表示不选择 0表示选中第一个
builder.setSingleChoiceItems(arr, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context,"您选择了"+arr[which],Toast.LENGTH_SHORT).show();
textView.setText(arr[which]);
}
});
//取消按钮
builder.setNegativeButton("取消",null);
//创建 显示
builder.create().show();
} else if (v.getId() == R.id.btn_four) {
// 多选对话框
builder = new AlertDialog.Builder(context);
//选择后的集合
List datas = new ArrayList<>();
//数据源
final String[] arr = {"胡萝卜炒肉","辣椒炒蛋","水煮活鱼","清蒸龙虾"};
//标题
builder.setTitle("请选择菜单:");
//图标
builder.setIcon(R.mipmap.a);
//内容 setMultiChoiceItems
// 数据源,默认选中哪几个, null 表示一个都不选
// new boolean[]{false, false, true, true}
builder.setMultiChoiceItems(arr, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
datas.add(arr[which]);
}else{
datas.remove(arr[which]);
}
}
});
//确定按钮 setPositiveButton
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context,"您选择了"+datas.toString(),Toast.LENGTH_SHORT).show();
textView.setText(datas.toString());
}
});
//取消
builder.setNeutralButton("取消",null);
//创建 显示
builder.create().show();
}if(v.getId() == R.id.btn_five){
// 适配器对话框 调用写的方法
builder = AlertSimplAdapter.getAlertBuilder(context);
//创建 显示
builder.create().show();
}
}
}
package com.example.myalertdialog;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AlertSimplAdapter {
//图片
private static final int[] arrImg = new int[]{
R.mipmap.a, R.mipmap.b, R.mipmap.c,
R.mipmap.d, R.mipmap.e, R.mipmap.f,
R.mipmap.g, R.mipmap.h, R.mipmap.i,
R.mipmap.a, R.mipmap.a, R.mipmap.a,
R.mipmap.a, R.mipmap.a, R.mipmap.a
};
//标题
private static final String[] arrTitle = {
"张三","李四","张三丰",
"小学群聊","周芷若","周先生",
"老领导","玖龙玺","阿道夫",
"张三","李四","张三丰",
"张三","李四","张三丰"
};
//内容
private static String[] arrContent =new String[]{
"早啊!","你在干啥","你最近在练什么武功",
"呼叫XXX家长","最近咋样","吃饭了吗?",
"啥时候有空喝茶呀?",".....","小伙子,学魔法吗?",
"早啊!","你在干啥","你最近在练什么武功",
"早啊!","你在干啥","你最近在练什么武功"
};
//时间
private static String[] arrTime = {
"刚刚","昨天","11-20",
"10-1","2023-11-20","17:30",
"9:00","10-1","2000-01-01",
"刚刚","昨天","11-20",
"刚刚","昨天","11-20"
};
public static AlertDialog.Builder getAlertBuilder(Context context){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
//标题
builder.setTitle("**通讯信息**");
//logo
builder.setIcon(R.mipmap.a);
//设置数据源 List extends Map> data
List