Android中Dialog对话框
布局文件xml:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context=".DialogActivity" > 11 12 <Button 13 android:id="@+id/plainDialog" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="普通Dialog" /> 17 18 <Button 19 android:id="@+id/plainDialogEvent" 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:text="Dialog按钮事件集中处理" /> 23 24 <Button 25 android:id="@+id/inputDialog" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:text="请输入框" /> 29 30 <Button 31 android:id="@+id/listDialog" 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content" 34 android:text="列表对话框" /> 35 36 <Button 37 android:id="@+id/radioDialog" 38 android:layout_width="match_parent" 39 android:layout_height="wrap_content" 40 android:text="单选对话框" /> 41 42 <Button 43 android:id="@+id/checkboxDialog" 44 android:layout_width="match_parent" 45 android:layout_height="wrap_content" 46 android:text="多选对话框" /> 47 48 <Button 49 android:id="@+id/diyDialog" 50 android:layout_width="match_parent" 51 android:layout_height="wrap_content" 52 android:text="自定义布局对话框" /> 53 54 </LinearLayout>
Activity文件:
普通的dialog:
1 private void plainDialogDemo() { 2 3 Button plainBtn = (Button) findViewById(R.id.plainDialog); 4 plainBtn.setOnClickListener(new OnClickListener() { 5 6 public void onClick(View v) { 7 8 new AlertDialog.Builder(DialogActivity.this) 9 .setTitle("删除") 10 .setMessage("确定删除指定数据") 11 .setPositiveButton("确定", 12 new DialogInterface.OnClickListener() { 13 14 @Override 15 public void onClick(DialogInterface dialog, 16 int which) { 17 Toast.makeText(getApplicationContext(), 18 "确定了", Toast.LENGTH_SHORT) 19 .show(); 20 } 21 }) 22 .setNegativeButton("取消", 23 new DialogInterface.OnClickListener() { 24 25 @Override 26 public void onClick(DialogInterface dialog, 27 int which) { 28 } 29 }).setCancelable(false).show(); 30 } 31 }); 32 }
效果如下:
输入文本框的dialog:
1 private void inputDialog() { 2 Button inputBtn = (Button) findViewById(R.id.inputDialog); 3 inputBtn.setOnClickListener(new OnClickListener() { 4 5 @Override 6 public void onClick(View v) { 7 // TODO Auto-generated method stub 8 final EditText et = new EditText(DialogActivity.this); 9 new AlertDialog.Builder(DialogActivity.this) 10 .setTitle("请输入数字") 11 .setView(et) 12 .setPositiveButton("确定", 13 new DialogInterface.OnClickListener() { 14 15 @Override 16 public void onClick(DialogInterface dialog, 17 int which) { 18 // TODO Auto-generated method stub 19 Toast.makeText(getApplicationContext(), 20 et.getText(), 21 Toast.LENGTH_SHORT).show(); 22 } 23 }).setNegativeButton("取消", null) 24 .setCancelable(false).show(); 25 } 26 }); 27 }
效果如下:
列表dialog:
private void listDialogDemo() {Button listBtn = (Button) findViewById(R.id.listDialog);listBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {final String[] names = { "C罗", "J罗", "H罗" };new AlertDialog.Builder(DialogActivity.this).setTitle("列表对话框").setItems(names, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {Toast.makeText(DialogActivity.this,names[which], Toast.LENGTH_SHORT).show();}}).setNegativeButton("取消", null).show();}});}
效果如下:
单选dialog:
1 private void radioDialogDemo() { 2 Button radioButton = (Button) findViewById(R.id.radioDialog); 3 radioButton.setOnClickListener(new OnClickListener() { 4 5 @Override 6 public void onClick(View v) { 7 8 final String[] names = { "C罗", "J罗", "H罗" }; 9 new AlertDialog.Builder(DialogActivity.this) 10 .setTitle("列表对话框") 11 .setSingleChoiceItems(names, 0, 12 new DialogInterface.OnClickListener() { 13 14 @Override 15 public void onClick(DialogInterface dialog, 16 int which) { 17 18 selecteName = names[which]; 19 } 20 }) 21 .setPositiveButton("确定", 22 new DialogInterface.OnClickListener() { 23 24 @Override 25 public void onClick(DialogInterface dialog, 26 int which) { 27 28 Toast.makeText(DialogActivity.this, 29 selecteName, Toast.LENGTH_SHORT) 30 .show(); 31 } 32 }).setNegativeButton("取消", null).show(); 33 } 34 }); 35 }
效果如下:
多选dialog:
1 private void checkDialogDemo() { 2 Button checkBtn = (Button) findViewById(R.id.checkboxDialog); 3 checkBtn.setOnClickListener(new OnClickListener() { 4 5 @Override 6 public void onClick(View v) { 7 final String[] names = { "C罗", "J罗", "H罗" }; 8 final boolean[] selected = new boolean[] { true, false, true }; 9 new AlertDialog.Builder(DialogActivity.this) 10 .setMultiChoiceItems( 11 names, 12 selected, 13 new DialogInterface.OnMultiChoiceClickListener() { 14 15 @Override 16 public void onClick(DialogInterface dialog, 17 int which, boolean isChecked) { 18 19 } 20 }) 21 .setPositiveButton("确定", 22 new DialogInterface.OnClickListener() { 23 24 @Override 25 public void onClick(DialogInterface dialog, 26 int which) { 27 StringBuilder sb = new StringBuilder( 28 "你选择了:"); 29 for (int i = 0; i < names.length; i++) { 30 if (selected[i]) { 31 sb.append(names[i]); 32 } 33 } 34 Toast.makeText(DialogActivity.this, 35 sb.toString(), 1).show(); 36 } 37 }).setNegativeButton("取消", null).show(); 38 } 39 }); 40 }
效果如下:
自定义dialog:
1 private void customDialogDemo() { 2 final AlertDialog dlg = new AlertDialog.Builder(this).create(); 3 dlg.show(); 4 Window window = dlg.getWindow(); 5 window.setContentView(R.layout.diylayout); 6 ImageButton ok = (ImageButton) window.findViewById(R.id.btnok); 7 ok.setOnClickListener(new View.OnClickListener() { 8 9 @Override 10 public void onClick(View v) { 11 Toast.makeText(getApplicationContext(), "关闭了", 12 Toast.LENGTH_SHORT).show(); 13 dlg.dismiss(); 14 } 15 }); 16 }
自定义布局:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <ImageView 7 android:id="@+id/dialogimg" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:layout_centerInParent="true" 11 android:src="@drawable/dialog_bg" /> 12 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_alignLeft="@id/dialogimg" 17 android:layout_alignTop="@id/dialogimg" 18 android:layout_marginLeft="50dp" 19 android:layout_marginTop="60dp" 20 android:text="自定义的dialog" /> 21 22 <ImageButton 23 android:id="@+id/btnok" 24 android:layout_width="30dp" 25 android:layout_height="30dp" 26 android:layout_alignRight="@id/dialogimg" 27 android:layout_alignTop="@id/dialogimg" 28 android:layout_marginRight="15dp" 29 android:layout_marginTop="15dp" 30 android:background="@drawable/close_dialog" /> 31 32 </RelativeLayout>
效果如下:
转载于:https://www.cnblogs.com/stayreal/p/3952202.html
Android中Dialog对话框相关推荐
- android 中dialog对话框,Android中的对话框dialog
普通对话框 单选对话框 多选对话框 进度条对话框 底部弹出框 1.普通对话框 this 代表当前类 最终继承Context 相当于是子类 getApplicationContext:直接返回的是Con ...
- android 中Dialog对话框及自定义Dialog的方法
// 重写方法,让他返回Dialog.// 以下是优化代码的替换,只创建一次,每次点击使用showDialog()的方法,不用每次都创建新的,可以减少内存使用@Overrideprotected Di ...
- android自定义dialog对话框,android的自定义dialog对话框实现
Android自定义dialog对话框 一:自定义dialog 如何填充布局 ①自定MyDialog类,在类里关联布局 public class MyDialog extends AlertDialo ...
- Android开发中Dialog对话框的使用
2011年12月01日09:34 来源: 博客园作者:诚实小郎君 编辑: 景保玉 我要评论(0) [IT168技术]Dialog是android开发过程中最常用到的组件之一,它包括以下几种类型: 警告 ...
- Android中Dialog的使用
上一篇博文讲到对话框popWindow的使用,这篇博文主要解说Dialog的使用. 1.什么是Dialog? Dialog就是对话框的一种方式! 在Android开发中.我们常常会须要在Android ...
- Android中Dialog数据的获取报错空指针问题
今天自定义Dialog,目的是需要在Dialog中输入EditText然后获取数据传递到需要的地方,最初的方法: OnCreat()方法中初始化view控件,然后使用.getText()来获取,这是平 ...
- 去除 element-ui 中 Dialog 对话框遮罩层的方法
在使用element-ui的Dialog时,默认会有遮罩层,想要去除遮罩层,官方文档给出了 modal 属性: modal 是一个布尔值,表示是否需要遮罩层: 然而,将 modal 赋值为 false ...
- Android中Dialog与DialogFragment的对比
2019独角兽企业重金招聘Python工程师标准>>> 最近学习对话框时发现有两种类型的可供使用,一种是Dialog,另一种则是Android 3.0 引入的基于Fragment的D ...
- android中常见对话框之一AlertDialog
在Android应用中,有多种对话框:Dialog.AlertDialog.ProgressDialog.时间.日期等对话框. (1)Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽 ...
最新文章
- 2022年十大AI预测
- Centos-6 编译安装实现LAMP,wordpress最新版
- Redis进阶-Redis使用建议一二事
- linux aemv7,无法在我的Ubuntu machin中安装“xlwings”
- 最短路弗洛伊德(Floyd)算法加保存路径
- git仓库创建后,由主支变成开发分支
- oracle 查询不能重复,oracle – 如何防止在选择查询中选择重复行?
- 有一分数序列求前20项和答案c语言,用c语言求解:有一分数序列2/1,3/2,5/3,8/5,13/8,21/13,…求这个数列的前20项之和....
- iphone保修期多久_小心!iPhone翻新机,黑机,妖机,1978机千万别买!
- python显示1000以内的斐波拉契数列_Python 实现斐波那契数列方法及其优化总结
- Egg 2.20.0 发布,阿里开源的企业级 Node.js 框架
- 软件测试高频面试题真实分享/网上银行转账是怎么测的,设计一下测试用例。
- 从0到1开发H5游戏
- 几个常用快速无损压缩算法性能比较
- 《AI进化论:解码人工智能商业场景与案例》----读书笔记
- tdr 定位公式_时域反射计TDR原理详细解析
- html文件中top什么意思,margin-top在html中的意思是什么
- iacr是什么会议_计算机国际安全顶级会议
- MySQL——数据库
- 游戏开发毛星云离世,他一直有个梦想:国产游戏能领跑世界
热门文章
- 正则表达式---重复
- Elasticsearch 2.3.0 重建索引
- ubuntu 内Grub2配置详解(转)
- 设置 ASP.NET 存储当前应用程序的临时文件(生成的源、编译了的程序集等)的目录的物理路径。...
- c语言表白代码颜色,C语言告白代码,一闪一闪亮晶晶~
- android handler的机制和原理_第一百八十回:Android中的Handler机制九
- linux 0755,linux audit审计(7-1)--读懂audit日志
- MySQL之数据库基本操作语句
- 05NumPy--5.3数组运算
- 鱼池正式宣布支持EIP-1559