AlertDialog的五种样式
2024-10-19 23:55:12
建立DialogActivity.java文件:
1 public class DialogActivity extends AppCompatActivity {
2 private Button BtnDialog1,BtnDialog2,BtnDialog3,BtnDialog4,BtnDialog5;
3 @Override
4 protected void onCreate(Bundle savedInstanceState) {
5 super.onCreate(savedInstanceState);
6 setContentView(R.layout.activity_dialog);
7 BtnDialog1=findViewById(R.id.btn_dialog1);
8 BtnDialog2=findViewById(R.id.btn_dialog2);
9 BtnDialog3=findViewById(R.id.btn_dialog3);
10 BtnDialog4=findViewById(R.id.btn_dialog4);
11 BtnDialog5=findViewById(R.id.btn_dialog5);
12 OnClick onClick=new OnClick();
13 BtnDialog1.setOnClickListener(onClick);
14 BtnDialog2.setOnClickListener(onClick);
15 BtnDialog3.setOnClickListener(onClick);
16 BtnDialog4.setOnClickListener(onClick);
17 BtnDialog5.setOnClickListener(onClick);
18 }
19
20 class OnClick implements View.OnClickListener{
21 @Override
22 public void onClick(View view) {
23 switch(view.getId()){
24 case R.id.btn_dialog1:
25 AlertDialog.Builder builder=new AlertDialog.Builder(DialogActivity.this);
26 builder.setTitle("请问:").setMessage("天哥的课程怎么样?")
27 .setIcon(R.drawable.zhanghao)//加图标
28 .setPositiveButton("不错", new DialogInterface.OnClickListener() {
29 @Override
30 public void onClick(DialogInterface dialogInterface, int i) {
31 //ToastUtil.showMsg(DialogActivity.this,"诚实的很");
32 Toast.makeText(DialogActivity.this, "有眼光", Toast.LENGTH_SHORT).show();
33 }
34 }).setNeutralButton("一般", new DialogInterface.OnClickListener() {
35 @Override
36 public void onClick(DialogInterface dialogInterface, int i) {
37 Toast.makeText(DialogActivity.this, "要不再看看?", Toast.LENGTH_SHORT).show();
38 }
39 }).setNegativeButton("不好", new DialogInterface.OnClickListener() {
40 @Override
41 public void onClick(DialogInterface dialogInterface, int i) {
42 Toast.makeText(DialogActivity.this, "请注意言辞", Toast.LENGTH_SHORT).show();
43 }
44 }).show();//默认样式
45 break;
46 case R.id.btn_dialog2:
47 String[] array2=new String[]{"女","男"};
48 AlertDialog.Builder builder2=new AlertDialog.Builder(DialogActivity.this);
49 builder2.setTitle("您的性别是:").setItems(array2, new DialogInterface.OnClickListener() {
50 @Override
51 public void onClick(DialogInterface dialogInterface, int i) {
52 Toast.makeText(DialogActivity.this, array2[i], Toast.LENGTH_SHORT).show();
53 }
54 }).show();
55 break;
56 case R.id.btn_dialog3:
57 String[] array3=new String[]{"女","男"};
58 AlertDialog.Builder builder3=new AlertDialog.Builder(DialogActivity.this);
59 builder3.setTitle("您的性别是:").setSingleChoiceItems(array3, 0, new DialogInterface.OnClickListener() {
60 //其中0是默认选择的位置
61 @Override
62 public void onClick(DialogInterface dialogInterface, int i) {
63 Toast.makeText(DialogActivity.this, array3[i], Toast.LENGTH_SHORT).show();
64 dialogInterface.dismiss();//设置了这个点击选项后就会消失
65 }
66 }).setCancelable(false).show();//单选框样式,.setCancelable(false)这个点其他任何位置警示框都不会消失
67 break;
68 case R.id.btn_dialog4:
69 String[] array4=new String[]{"抽烟","喝酒","烫头"};
70 boolean[] isSelected=new boolean[]{false,true,true};//默认选中哪个哪个就是true
71 AlertDialog.Builder builder4=new AlertDialog.Builder(DialogActivity.this);
72 builder4.setTitle("您的兴趣是:").setMultiChoiceItems(array4, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
73 @Override
74 public void onClick(DialogInterface dialogInterface, int i, boolean b) {
75 Toast.makeText(DialogActivity.this, array4[i]+":"+b, Toast.LENGTH_SHORT).show();
76 }
77 }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
78 @Override
79 public void onClick(DialogInterface dialogInterface, int i) {
80 //没有点击事件
81 }
82 }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
83 @Override
84 public void onClick(DialogInterface dialogInterface, int i) {
85 //没有点击事件
86 }
87 }).show();//多选框
88 break;
89 case R.id.btn_dialog5:
90 AlertDialog.Builder builder5=new AlertDialog.Builder(DialogActivity.this);
91 View view1=LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog,null);
92 EditText etUsername=view1.findViewById(R.id.et_username);
93 EditText stPassword=view1.findViewById(R.id.et_password);
94 Button btnLogin=view1.findViewById(R.id.btn_login);
95 btnLogin.setOnClickListener(new View.OnClickListener() {
96 @Override
97 public void onClick(View view) {
98 //没有点击事件
99 }
100 });
101 builder5.setTitle("请先进行登录").setView(view1).show();//自定义警示框
102 break;
103 }
104 }
105 }
106 }
对应的activity_dialog.xml文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="match_parent"
3 android:layout_height="match_parent"
4 android:orientation="vertical">
5 <Button
6 android:id="@+id/btn_dialog1"
7 android:layout_width="match_parent"
8 android:layout_height="wrap_content"
9 android:text="style1"
10 android:textAllCaps="false"/>
11 <Button
12 android:id="@+id/btn_dialog2"
13 android:layout_width="match_parent"
14 android:layout_height="wrap_content"
15 android:text="style2"
16 android:textAllCaps="false"/>
17 <Button
18 android:id="@+id/btn_dialog3"
19 android:layout_width="match_parent"
20 android:layout_height="wrap_content"
21 android:text="style3"
22 android:textAllCaps="false"/>
23 <Button
24 android:id="@+id/btn_dialog4"
25 android:layout_width="match_parent"
26 android:layout_height="wrap_content"
27 android:text="style4"
28 android:textAllCaps="false"/>
29 <Button
30 android:id="@+id/btn_dialog5"
31 android:layout_width="match_parent"
32 android:layout_height="wrap_content"
33 android:text="style5"
34 android:textAllCaps="false"/>
35 </LinearLayout>
还有设置样式的layout_dialog.xml文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="match_parent"
3 android:layout_height="match_parent"
4 android:orientation="vertical"
5 android:padding="20dp">
6 <EditText
7 android:id="@+id/et_username"
8 android:layout_width="match_parent"
9 android:layout_height="wrap_content"
10 android:hint="username"
11 android:maxLines="1"/>
12 <EditText
13 android:id="@+id/et_password"
14 android:layout_width="match_parent"
15 android:layout_height="wrap_content"
16 android:hint="password"
17 android:inputType="textPassword"
18 android:maxLines="1"
19 android:layout_marginTop="10dp"/>
20 <Button
21 android:id="@+id/btn_login"
22 android:layout_width="match_parent"
23 android:layout_height="wrap_content"
24 android:text="login"
25 android:textAllCaps="false"
26 android:layout_marginTop="10dp"/>
27 </LinearLayout>
最新文章
- pipedata3d User Guide
- 获取设备的mac地址可靠的方法
- HTTP协议学习---(六)缓存
- 解决客户 IE 浏览器";兼容性视图";设置带来的问题
- python操作mongodb之二聚合查询
- Timer wheel etc.
- iOS学习之UINavigationController
- Java Map排序
- java提高数据库访问效率代码优化
- statspack系列8
- Ubuntu下安装nvidia显卡驱动
- HTML5新增的属性和废除的属性
- lsof命令详解
- 关于UTF8文件带BOM头可能会引起的错误解析
- 关于stm32的数据类型
- SAS 对数据的拼接与串接
- bzoj1708:[Usaco2007 Oct]Money奶牛的硬币(完全背包
- python 数据库查询
- 延续(continuation)
- iOS之UITextField限制字数
热门文章
- Servlet-IDEA菜单生成Servlet程序
- linux下查看 SELinux状态及关闭SELinux
- Nginx怎么处理请求的?
- opencvsharp 根据row方向和面积筛选连通域的两种方式
- 013 Linux 搞懂「文件所属者更改及权限的赋予」从未如此简单(chmod、chgrp、chown)
- nodejs 文件上传服务端实现
- 修改注册表使win server 2012R2开机进入桌面而不是开始界面
- Centos 系统目录概述
- 《Effective Python》笔记——第3章 类与继承
- Failed to connect to raw.githubusercontent.com 443 解决方案