@
一般使用
//构造AlertDialog.Builder对象
AlertDialog.Builder defaultBuilder = new AlertDialog.Builder(MainActivity.this);
//调用setTitle、setMessage、setIcon等方法构造对话框的标题、信息和图标等内容;
//标题
defaultBuilder.setTitle("默认样式");
//提示信息
defaultBuilder.setMessage("这是什么样式?");
//调用setPositive/Negative/NeutralButton()方法设置选项 有正面按钮、负面按钮、中立按钮;
//正面按钮 第一个参数提示信息 第二个参数实现监听接口
defaultBuilder.setPositiveButton("默认样式",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"选对了",Toast.LENGTH_SHORT).show();
}
});
//负面按钮
defaultBuilder.setNegativeButton("多选样式", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"选错了",Toast.LENGTH_SHORT).show();
}
});
//中立按钮
defaultBuilder.setNeutralButton("我再想想", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"那你再想想",Toast.LENGTH_SHORT).show();
}
});
//显示
defaultBuilder.show();
普通列表
//列表
AlertDialog.Builder tableBuilder = new AlertDialog.Builder(MainActivity.this);
final String[] course = {"C++","Java","Go","php","Pyhton"};
tableBuilder.setTitle("你喜欢那个语言?");
tableBuilder.setItems(course, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,course[which],Toast.LENGTH_SHORT).show();
}
});
tableBuilder.show();
单选列表
final String[] sex = {"男","女"};
AlertDialog.Builder singleBuilder = new AlertDialog.Builder(MainActivity.this);
singleBuilder.setTitle("请选择性别");
//setSingleChoiceItems 单选
//只能选中一个 第二个参数是默认选中哪一个
singleBuilder.setSingleChoiceItems(sex,0, new DialogInterface.OnClickListener() {
//第二个参数代表选中那个下标的返回值
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,sex[which],Toast.LENGTH_SHORT).show();
dialog.dismiss();//关闭对话框
}
});
singleBuilder.show();
多选列表
final String[] book = {"从删库到跑路","深入理解计算机系学生秃头的原因","程序员的构造和解释","算法导论论不懂","计算机网络联不通"};
boolean[] begin = {false,false,false,false,false};//初始状态为全部不选中
ArrayList<String> bookList = new ArrayList<String>();
AlertDialog.Builder multipleAlertDialog = new AlertDialog.Builder(MainActivity.this);
multipleAlertDialog.setTitle("想要读什么?");
//setMultiChoiceItems 多选
multipleAlertDialog.setMultiChoiceItems(book, begin, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
bookList.add(book[which]);
Toast.makeText(MainActivity.this,"任意选中就true",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"任意取消就false",Toast.LENGTH_SHORT).show();
bookList.remove(book[which]);
}
//Toast.makeText(MainActivity.this,book[which],Toast.LENGTH_SHORT).show();
}
});
multipleAlertDialog.setPositiveButton("选好了", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,bookList.toString(),Toast.LENGTH_SHORT).show();
}
});
multipleAlertDialog.setNeutralButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"不准备读一读吗?",Toast.LENGTH_SHORT).show();
}
});
multipleAlertDialog.show();
自定义弹出框
定义对话框布局
<?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:background="#FFFFFFFF"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="标题"
android:textSize="30sp"
android:textColor="#000"
android:layout_marginTop="20dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="自定义的AlertDialog"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginTop="20dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:background="#414E5E"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_weight="1"
android:text="取消"
android:backgroundTint="#F8F3F3"
android:textColor="#0781B8"
android:textSize="20sp"
android:layout_height="wrap_content" />
<View
android:layout_width="1dp"
android:background="#E4E5E6"
android:layout_height="match_parent" />
<Button
android:id="@+id/btn_save"
android:layout_width="0dp"
android:layout_weight="1"
android:backgroundTint="#F8F3F3"
android:text="保存"
android:textColor="#0781B8"
android:textSize="20sp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
设置布局到对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//先创建AlertDialog对象
AlertDialog dialog = builder.create();
//初始化view
View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.alertdialog_custom,null);
//设置弹出页面 为自定义布局
dialog.setView(dialogView);
dialog.show();
//剩下的当成正常页面操作
Button btnCancel,btnSave;
btnCancel = dialogView.findViewById(R.id.btn_cancel);
btnSave = dialogView.findViewById(R.id.btn_save);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"你选择了取消",Toast.LENGTH_SHORT).show();
dialog.dismiss();//关闭对话框
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"你选择了保存",Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
浙公网安备 33010602011771号