对话框
-
对话框(Dialog)是一种常用的 UI 组件,它主要用于显示信息、接收用户操作反馈
-
对话框可以包含各种元素,但是主要还是以文本、按钮为主,其次是列表
-
其中,基础对话框是 Android 中最简单的对话框,而后是进度对话框、自定义对话框等
一、UI 线程问题
1、UI 线程中创建对话框
- UI 线程中创建对话框会抛出异常
try {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("普通对话框");
builder.setMessage("确定退出吗?");
builder.setPositiveButton("确定", (dialog, which) -> {
Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("取消", (dialog, which) -> {
Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
} catch (Exception e) {
e.printStackTrace();
}
- 输出结果
java.lang.RuntimeException: Can't create handler inside thread Thread[Thread-2,5,main] that has not called Looper.prepare()
2、UI 线程中显示对话框
- UI 线程中显示对话框会抛出异常
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("普通对话框");
builder.setMessage("确定退出吗?");
builder.setPositiveButton("确定", (dialog, which) -> {
Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("取消", (dialog, which) -> {
Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
});
AlertDialog alertDialog = builder.create();
new Thread(() -> {
try {
alertDialog.show();
} catch (Exception e) {
e.printStackTrace();
}
}).start();
- 输出结果
java.lang.RuntimeException: Can't create handler inside thread Thread[Thread-2,5,main] that has not called Looper.prepare()
3、UI 线程中关闭对话框
- UI 线程中关闭对话框不会抛出异常
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("普通对话框");
builder.setMessage("确定退出吗?");
builder.setPositiveButton("确定", (dialog, which) -> {
Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("取消", (dialog, which) -> {
Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
new Thread(() -> {
try {
Thread.sleep(5 * 1000);
alertDialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}).start();
二、外部取消
1、普通对话框冲突问题外部取消
- 通过
setCancelable(boolean cancelable)
方法设置普通对话框是否可以通过点击外部区域来取消
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("普通对话框");
builder.setMessage("确定退出吗?");
builder.setPositiveButton("确定", (dialog, which) -> {
Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("取消", (dialog, which) -> {
Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
});
builder.setCancelable(false); // 不建议使用
AlertDialog alertDialog = builder.create();
alertDialog.show();
2、单选对话框冲突问题外部取消
-
通过
setCancelable(boolean cancelable)
方法设置单选对话框是否可以通过点击外部区域来取消 -
但是不建议对单选对话框禁止点击外部区域来取消,因为用户可能误触发了单选对话框,本来不想做选择
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("单选对话框");
String[] items = {"物理", "化学", "生物", "历史"};
builder.setSingleChoiceItems(items, -1, (dialog, which) -> {
Toast.makeText(this, "选择了:" + items[which], Toast.LENGTH_SHORT).show();
dialog.dismiss();
});
builder.setCancelable(false); // 不建议使用
AlertDialog alertDialog = builder.create();
alertDialog.show();
3、多选对话框冲突问题外部取消
-
通过
setCancelable(boolean cancelable)
方法设置多选对话框是否可以通过点击外部区域来取消 -
但是不建议对多选对话框禁止点击外部区域来取消,因为用户可能误触发了多选对话框,本来不想做选择
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("多选对话框");
String[] items = {"胡萝卜", "白菜", "菠菜", "土豆", "黄瓜", "西红柿"};
boolean[] check = {false, false, false, false, false, false};
builder.setMultiChoiceItems(items, check, (dialog, which, isChecked) -> {
if (isChecked) {
check[which] = true;
Toast.makeText(this, "你选中了:" + items[which], Toast.LENGTH_SHORT).show();
} else {
check[which] = false;
Toast.makeText(this, "你取消了:" + items[which], Toast.LENGTH_SHORT).show();
}
});
builder.setCancelable(false); // 不建议使用
AlertDialog alertDialog = builder.create();
alertDialog.show();
4、列表对话框冲突问题外部取消
-
通过
setCancelable(boolean cancelable)
方法设置列表对话框是否可以通过点击外部区域来取消 -
但是不建议对列表对话框禁止点击外部区域来取消,因为用户可能误触发了列表对话框,本来不想做选择
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("课程安排:");
String[] items = {"第一节(数学)", "第二节(地理)", "第三节(历史)", "第四节(美术)"};
builder.setItems(items, (dialog, which) -> {
Toast.makeText(this, items[which], Toast.LENGTH_SHORT).show();
});
builder.setCancelable(false);
AlertDialog alertDialog = builder.create();
alertDialog.show();
三、冲突问题
1、单选对话框冲突问题
- setSingleChoiceItems 方法与 setMessage 方法显示区域互相冲突,同时使用时,setMessage 方法设置的内容会覆盖掉 setSingleChoiceItems 方法设置的内容
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("单选对话框");
builder.setMessage("这是单选对话框");
String[] items = {"物理", "化学", "生物", "历史"};
builder.setSingleChoiceItems(items, -1, (dialog, which) -> {
Toast.makeText(this, "选择了:" + items[which], Toast.LENGTH_SHORT).show();
dialog.dismiss();
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
2、多选对话框冲突问题
- setMultiChoiceItems 方法与 setMessage 方法显示区域互相冲突,同时使用时,setMessage 方法设置的内容会覆盖掉 setMultiChoiceItems 方法设置的内容
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("多选对话框");
builder.setMessage("这是多选对话框");
String[] items = {"胡萝卜", "白菜", "菠菜", "土豆", "黄瓜", "西红柿"};
boolean[] check = {false, false, false, false, false, false};
builder.setMultiChoiceItems(items, check, (dialog, which, isChecked) -> {
if (isChecked) {
check[which] = true;
Toast.makeText(this, "你选中了:" + items[which], Toast.LENGTH_SHORT).show();
} else {
check[which] = false;
Toast.makeText(this, "你取消了:" + items[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
3、列表对话框冲突问题
- setItems 方法与 setMessage 方法显示区域互相冲突,同时使用时,setMessage 方法设置的内容会覆盖掉 setItems 方法设置的内容
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("课程安排:");
builder.setMessage("这是列表对话框");
String[] items = {"第一节(数学)", "第二节(地理)", "第三节(历史)", "第四节(美术)"};
builder.setItems(items, (dialog, which) -> {
Toast.makeText(this, items[which], Toast.LENGTH_SHORT).show();
});
builder.setCancelable(false);
AlertDialog alertDialog = builder.create();
alertDialog.show();
四、dismiss 方法与 hide 方法
1、基本介绍
-
dismiss 方法:用于完全关闭并移除 AlertDialog,一旦调用此方法,AlertDialog 将不再显示在屏幕上,且相关的资源会被移除
-
hide 方法:用于关闭 AlertDialog,一旦调用此方法,AlertDialog 将不再显示在屏幕上,但相关的资源不会被移除
2、演示
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("普通对话框");
builder.setMessage("确定退出吗?");
builder.setPositiveButton("确定", (dialog, which) -> {
Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("取消", (dialog, which) -> {
Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
Handler handler = new Handler();
handler.postDelayed(() -> alertDialog.hide(), 5 * 1000);
标签:Toast,hide,对话框,builder,dismiss,alertDialog,AlertDialog,show
From: https://blog.csdn.net/weixin_52173250/article/details/143085549