1、调用AlertDialog的静态内部类Builder创建AlertDialog.Builder的对象。
2、调用AlertDialog.Builder的setTitle()和setIcon()方法分别设置AlertDialog对话框的标题名称和图标。
3、调用AlertDialog.Builder的setMessage()、setSingleChoiceItems()或者setMultiChoiceItems()方法设置AlertDialog对话框的内容为简单文本、单选列表或者为多选列表。
4、调用AlertDialog.Builder的setPositiveButton()和setNegativeButton()方法设置AlertDialog对话框的确定和取消按钮。
5、调用AlertDialog.Builder的create()方法创建AlertDialog对象。
6、调用AlertDialog对象的show()方法显示该对话框。
7、调用AlertDialog对象的dismiss()方法取消该对话框。
普通对话框+单选对话框+多选对话框+自定义对话框小实例
package haue.edu.dialog_test;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//当退出当前页面时显示对话框
@Override
public void onBackPressed() {
//super.onBackPressed();
// AlertDialog dialog;
// AlertDialog.Builder builder=new AlertDialog.Builder(this);
// builder.setTitle("通知!");
// builder.setIcon(R.mipmap.ic_launcher);
// builder.setMessage("就要退出了");
// builder.setPositiveButton("去意已决", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// dialog.dismiss();
// MainActivity.this.finish();//关闭页面
// }
// });
// builder.setNegativeButton("再想想", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// dialog.dismiss();
// }
// });
// dialog=builder.create();
// dialog.show();
//第二种方法
AlertDialog dialog;
AlertDialog.Builder builder=new AlertDialog.Builder(this)
.setTitle("警告")
.setIcon(R.mipmap.ic_launcher)
.setNegativeButton("考虑一下", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("去意已决", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
}
});
dialog=builder.create();
dialog.show();
//设置按钮文本颜色
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(Color.GREEN);
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.CYAN);
}
}
---------------------------------------------------------------------------------
//单选对话框(字体大小)
public void sizechange(View view) {
final int[] size={10,30,50};
//保存用户选择的是几号字体值是数组的下标
text_size = 1;
AlertDialog dialog;
AlertDialog.Builder builder=new AlertDialog.Builder(this)
.setTitle("请选择字号")
.setIcon(R.mipmap.ic_launcher)
.setSingleChoiceItems(new String[]{"小号", "中号", "大号"}, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { //which是下标
text_size=which;
}
})
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
tv_infor.setTextSize(size[text_size]);
}
})
.setNegativeButton("candle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog=builder.create();
dialog.show();
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//多选对话框
//弹出多选对话框
public void select_hobby(View view) {
final String[] hobby={"reading","singing","sleeping"};
final boolean[]checked={false,false,false};
AlertDialog dialog;
AlertDialog.Builder builder=new AlertDialog.Builder(this)
.setTitle("请选择爱好")
.setIcon(R.mipmap.ic_launcher)
.setMultiChoiceItems(hobby, checked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checked[which]=isChecked;//将第which个选项的状态设置为isChecked
}
})
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
String result="";//用户最终的选择,初始化为空
for(int i=0;i<checked.length;i++){
if(checked[i]){
result+=hobby[i]+" ";
}
}
tv_infor.setText(result);
}
})
.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog=builder.create();
dialog.show();
}
可能会用到的方法:onBackpressed()
finish()方法
dialog.getButton()可以改变dialog中的按钮的颜色