建立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>

AlertDialog的五种样式的更多相关文章

  1. 5种样式实现div容器中三图摆放实例对比说明

    代码地址如下:http://www.demodashi.com/demo/11593.html 效果演示: demo点查看效果 需求说明: 如下图所示为设计图,希望在图片上传无规则无规律的情况下实现设 ...

  2. html table表格导出excel的方法 html5 table导出Excel HTML用JS导出Excel的五种方法 html中table导出Excel 前端开发 将table内容导出到excel HTML table导出到Excel中的解决办法 js实现table导出Excel,保留table样式

    先上代码   <script type="text/javascript" language="javascript">   var idTmr; ...

  3. CSS 四种样式表 六种规则选择器 五种常用样式属性

    新的html程序要在VS中编写了,在vs中安装ASP.NET和Web开发,并用ASP.NET Web 应用程序(.NETFramework)创建一个网页程序.添加一个html页 后面的代码都是在htm ...

  4. Android经常使用的五种弹出对话框

    一个Android开发中经常使用对话框的小样例,共同拥有五种对话框:普通弹出对话框,单选对话框,多选对话框,输入对话框及进度条样式对话框: <LinearLayout xmlns:android ...

  5. 提高CSS文件可维护性的五种方法

    当完成一项前端的工作之后,许多人都会忘记该项目的结构与细节.然而代码并不是马上就能完全定型,在余下的时间里还有不断的维护工作,而这些工作也许不会是你自己完成.所以,结构优良的代码能很大程度上优化它的可 ...

  6. socket编程五种模型

    客户端:创建套接字,连接服务器,然后不停的发送和接收数据. 比较容易想到的一种服务器模型就是采用一个主线程,负责监听客户端的连接请求,当接收到某个客户端的连接请求后,创建一个专门用于和该客户端通信的套 ...

  7. Android特效 五种Toast详解

    Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失.而且Toast主要用于向用户显示提示消 ...

  8. android五种布局模式

    Android布局是应用界面开发的重要一环,在Android中,共有五种布局方式,分别是:LinearLayout (线性布局),FrameLayout(框架布局),AbsoluteLayout(绝对 ...

  9. CSS-三栏响应式布局(左右固宽,中间自适应)的五种方法

    代码: <!-- 1 float --> <h3 class="block">第一种方法-float</h3> <div class=&q ...

随机推荐

  1. 学习Java第13天

    今天选择数据库选了半天,Oracle,MySQL,SQL sever太难了,安装了又被图形界面,Linux虚拟机所困扰 明天尽量完成数据库安装 只能说是找视频资料和安装教程了.

  2. Linux中date命令用法

    1.以下是服务器现在的时间,当前时间的各种表示方法,表示成自己想要的时间格式,后面的范例将会在这个时间基础之上进行演示,同时这也是熟练掌握后面各种date命令的前提,请读者注意 [root@RHEL6 ...

  3. 如何在pyqt中给无边框窗口添加DWM环绕阴影

    前言 在之前的博客<如何在pyqt中通过调用SetWindowCompositionAttribute实现Win10亚克力效果>中,我们实现了窗口的亚克力效果,同时也用SetWindowC ...

  4. IntelliJ IDEA 学习笔记 - 修改编码

    感谢原文作者:codeke 原文链接:https://blog.csdn.net/cgl125167016/article/details/78666432 仓库:https://github.com ...

  5. Spring Boot配置多个DataSource (转)

    使用Spring Boot时,默认情况下,配置DataSource非常容易.Spring Boot会自动为我们配置好一个DataSource. 如果在application.yml中指定了spring ...

  6. Java&Tomcat环境变量配置

    版本匹配: Java PATH环境变量.作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是否能找到相应的命令程序.我们需要把 jdk安装目录下的bin目录增加 ...

  7. 基于Docker部署4.2 版本的zabbix监控平台

    准备工作 两台VMware 虚拟机 一台充当zabbix server(安装docker)ip:192.168.73.133 一台充当zabbix agent(安装docker)ip:192.168. ...

  8. js trim()方法

    从字符串中移除前导空格.尾随空格和行终止符. 语法 stringObj.trim() 参数 stringObj 必选.String 对象或字符串.trim 方法不修改该字符串. 返回值 已移除前导空格 ...

  9. PHP中的一些常用函数收集

    <?php //===============================时间日期=============================== //y返回年最后两位,Y年四位数,m月份数字 ...

  10. spring 注解注入bean

    通过注解方式注入bean,需要在配置类下注入bean 第一步,配置扫描文件夹 首先要在spring.xml中配置需要扫描的配置类 <context:componenet-scan base-pa ...