建立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. Redis Hyperloglog的原理及数学理论的通俗理解

    redis中有一种数据格式,hyperloglog,本文就此数据结构的作用.redis的实现及其背后的数学原理作一个整理.当然本文不包含任何数学公式,而是希望用直观的例子帮大家理解. 主要内容如下: ...

  2. 『无为则无心』Python基础 — 43、文件备份的实现

    目录 1.需求 2.步骤 3.代码实现 (1)接收用户输入目标文件名 (2)规划备份文件名 (3)备份文件写入数据 (4)思考 (5)完整编码 4.再来一个小练习 1.需求 用户输入当前目录下任意文件 ...

  3. linux 环境变量配置方式

    linux 环境变量可以在多个文件中配置 说明: linux bash 运行模式分为两种: login shell 和non-login shell, 两种登录模式启动是加载的配置文件不一样. 1. ...

  4. CF1574F Occurrences

    考虑什么样的串是合法的. 直接考虑比较抽象,考虑具象化这个问题. 容易发现一个字符串的限制就相当于如果出现了其中一个字符 \(a_i = c\),那么 \(s\) 中 \(c\) 前 \(i - 1\ ...

  5. JS 数据类型与运算符

    以下内容均整理自:廖雪峰老师的JS教程 非常感谢廖老师! 统一使用var声明即可,JS会自动判断类型. 数据类型 1. Number JavaScript不区分整数和浮点数,统一用Number表示,以 ...

  6. Td 内容不换行,超过部分自动截断,用...表示

    转载请注明来源:https://www.cnblogs.com/hookjc/ <table width="200px" style="table-layout:f ...

  7. UITabBarController的基本使用

  8. js添加元素代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Idea快捷键---根据自己使用情况持续更新

    查看接口的实现类 -->ctrl+alt+b 查看继承关系 -->ctrl+h 快速查看上次查看代码的位置: -->ctrl+alt+方向键(注意与intel显卡快捷键的冲突,如有冲 ...

  10. oracle查看当前正在使用的数据库

    select name from V$DATABASE; 也可以用 select SYS_CONTEXT('USERENV','INSTANCE_NAME') from dual;