您的位置:首页 > 科技 > 能源 > 北京疫情依然严重_企业文化展厅设计方案_买链接官网_百度浏览器下载

北京疫情依然严重_企业文化展厅设计方案_买链接官网_百度浏览器下载

2024/11/17 20:17:30 来源:https://blog.csdn.net/weixin_52173250/article/details/143085549  浏览:    关键词:北京疫情依然严重_企业文化展厅设计方案_买链接官网_百度浏览器下载
北京疫情依然严重_企业文化展厅设计方案_买链接官网_百度浏览器下载

对话框

  1. 对话框(Dialog)是一种常用的 UI 组件,它主要用于显示信息、接收用户操作反馈

  2. 对话框可以包含各种元素,但是主要还是以文本、按钮为主,其次是列表

  3. 其中,基础对话框是 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、单选对话框外部取消
  1. 通过 setCancelable(boolean cancelable) 方法设置单选对话框是否可以通过点击外部区域来取消

  2. 但是不建议对单选对话框禁止点击外部区域来取消,因为用户可能误触发了单选对话框,本来不想做选择

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、多选对话框外部取消
  1. 通过 setCancelable(boolean cancelable) 方法设置多选对话框是否可以通过点击外部区域来取消

  2. 但是不建议对多选对话框禁止点击外部区域来取消,因为用户可能误触发了多选对话框,本来不想做选择

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、列表对话框外部取消
  1. 通过 setCancelable(boolean cancelable) 方法设置列表对话框是否可以通过点击外部区域来取消

  2. 但是不建议对列表对话框禁止点击外部区域来取消,因为用户可能误触发了列表对话框,本来不想做选择

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、基本介绍
  1. dismiss 方法:用于完全关闭并移除 AlertDialog,一旦调用此方法,AlertDialog 将不再显示在屏幕上,且相关的资源会被移除

  2. 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);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com