case R.id.button4: //多选按钮
showMultiChoiceDialog();
break;
case R.id.button5: //等待
showWaitingDialog();
break;
case R.id.button6: //进度条
showProgressDialog();
break;
case R.id.button7: //编辑
showInputDialog();
break;
default:
break;
}
}
//三个按钮
private void showMultiBtnDialog() {
AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainActivity.this);
//设置对话框图标
normalDialog.setIcon(R.drawable.ic_launcher);
//设置对话框标题
normalDialog.setTitle(“对话框标题”);
//设置对话框消息提示
normalDialog.setMessage(“对话框内容提示”);
//按钮一
normalDialog.setPositiveButton(“按钮1”,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, “按钮1”, Toast.LENGTH_LONG).show();
}
}
);
//按钮二
normalDialog.setNeutralButton(“按钮2”,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, “按钮2”, Toast.LENGTH_LONG).show();
}
}
);
//按钮三
normalDialog.setNegativeButton(“按钮3”,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, “按钮3”, Toast.LENGTH_LONG).show();
}
}
);
normalDialog.show();
}
//两个按钮
private void showNorMalDialog() {
AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainActivity.this);
//设置对话框图标
normalDialog.setIcon(R.drawable.ic_launcher);
//设置对话框标题
normalDialog.setTitle(“对话框标题”);
//设置对话框消息提示
normalDialog.setMessage(“对话框内容提示”);
//setPositiveButton确定按钮
normalDialog.setPositiveButton(“确定”,
new Dialo 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 gInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, “确定按钮”, Toast.LENGTH_LONG).show();
}
}
);
//setNegativeButton取消按钮
normalDialog.setNegativeButton(“取消”,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, “取消按钮”, Toast.LENGTH_LONG).show();
}
}
);
//显示
normalDialog.show();
}
}
列表:
private void showListDialog() {
final String items[] = {“列表1”, “列表2”, “列表3”, “列表4”};
AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainActivity.this);
//设置对话框图标
normalDialog.setIcon(R.drawable.ic_launcher);
//设置对话框标题
normalDialog.setTitle(“列表”);
normalDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// which 下标从0开始
Toast.makeText(MainActivity.this, “你点击了” + items[which], Toast.LENGTH_SHORT).show();
}
});
normalDialog.show();
}
单选按钮:
int a;
private void showSingleChoiceDialog() {
final String items[] = {“单选1”, “单选2”, “单选3”, “单选4”};
a = -1;
AlertDialog.Builder dia = new AlertDialog.Builder(MainActivity.this);
//设置对话框图标
dia.setIcon(R.drawable.ic_launcher);
//设置对话框标题
dia.setTitle(“单选按钮”);
//第二个参数默认选项,此处设置为1,默认选中第二个
dia.setSingleChoiceItems(items, 1,
new DialogInterface.OnClickListener() {
//a为选中项的下标
@Override
public void onClick(DialogInterface dialog, int which) {
a = which;
}
}
);
dia.setPositiveButton(“确定”,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(a != -1){
Toast.makeText(MainActivity.this, “你选择了” + items[a], Toast.LENGTH_SHORT).show();
}
}
}
);
dia.show();
}
多选:
ArrayList list = new ArrayList();
private void showMultiChoiceDialog() {
final String items[] = {“多选1”, “多选2”, “多选3”, “多选4”};
AlertDialog.Builder dia = new AlertDialog.Builder(MainActivity.this);
//设置默认选中的选项,全为false默认均未选中
final boolean init[] = {false, false, false, false};
list.clear();
//设置对话框图标
dia.setIcon(R.drawable.ic_launcher);
//设置对话框标题
dia.setTitle(“多选按钮”);
dia.setMultiChoiceItems(items, init,
new DialogInterface.OnMultiChoiceClickListener() {
//判断有哪些为选中状态
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
list.add(which);
}else{
list.remove(which);
}
}
}
);
dia.setPositiveButton(“确定”,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int size = list.size();
String str = “”;
for(int i = 0; i < size; i++){
str += items[list.get(i)]+", ";
}
Toast.makeText(MainActivity.this, “你选中了” + str, Toast.LENGTH_SHORT).show();
}
}
);
dia.show();
}
等待:
private void showWaitingDialog() {
ProgressDialog dia = new ProgressDialog (MainActivity.this);
dia.setTitle(“等待”);
dia.setMessage(“等待中”);
dia.setIndeterminate(true);
//@setCancelable 为使屏幕不可点击,设置为不可取消false
dia.setCancelable(false);
dia.show();
}
进度条
private void showProgressDialog() {
final int max = 100;
final ProgressDialog dia = new ProgressDialog (MainActivity.this);
//设置初始进度条
dia.setProgress(0);
dia.setTitle(“进度条”);
//设置样式(水平进度条)
dia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置进度最大值
dia.setMax(max);
dia.show();
//模拟进度条增加的过程
//开启一个新线程,每个100ms,进程+1
new Thread(new Runnable(){
@Override
public void run() {
int p = 0;
while(p < max){
try {
Thread.sleep(100);
p++;
dia.setProgress§;
} catch (Exception e) {
Log.i(“Dialog”, “线程异常”);