Android之Builder对话框的一些常用方式
原文:
http://blog.csdn.net/kkfdsa132/article/details/6322835
Android为我们提供几种对话框,主要有:AlertDialog、ProgressDialog、DataPickerDialog、TimePickerDialog。
AlertDialog,是具有0-3个按钮,还可以可以放置选项、复选框单选框或自定义布局等,以达到与用户交互的效果。
AlertDialog中 最常用的莫过于Builder对话框。下面,列举出它常用的几种方式:1.简单的Builder;2.带控件的Builder;3.自定义布局的Builder;4.嵌套的Builder
1)简单的Builder
- new AlertDialog.Builder(AlertDialog_Builder.this) //Content上下文
- .setIcon(android.R.drawable.ic_dialog_alert) //图标,android.R开头表示Android提供的资源库
- .setTitle("简单的Builder:显示文本内容") //标题
- .setMessage(et_show.getText()) //要显示的内容
- .show(); //显示
2)带控件的Builder
- final CharSequence[] items ={"Black", "Red", "Blue", "Yellow"};
- final int[] color = {Color.BLACK, Color.RED, Color.BLUE, Color.YELLOW};
- final StringBuffer selectedItem = new StringBuffer("0");
- new AlertDialog.Builder(AlertDialog_Builder.this)
- .setIcon(android.R.drawable.ic_dialog_info)
- .setTitle("带Button的Builder:显示文本内容")
- //.setMessage(et_show.getText())
- .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- selectedItem.delete(0, selectedItem.length()-1);
- selectedItem.append(which);
- }
- })
- .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {//添加一个Button并注册其响应事件
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- int colorID = Integer.parseInt(selectedItem.toString());
- et_show.setBackgroundColor(color[colorID]);
- }
- })
- .setNegativeButton("清除", new DialogInterface.OnClickListener() { //注意,这个是SetNegativeButton,上面那个是SetPositiveButton
- //不能重复名字,否则最后那个会覆盖前面那个,也就是说每个Buttion只显示一个。
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- et_show.setText("");
- }
- })
- .setNeutralButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- dialog.cancel();
- }
- })
- .show();
3)自定义布局的Builder
a.代码
- final LayoutInflater layout = LayoutInflater.from(AlertDialog_Builder.this); //用于获取,要使用final修饰
- final View customView = layout.inflate(R.layout.alter_text, null); //创建自定义的View,要使用final修饰
- new AlertDialog.Builder(AlertDialog_Builder.this)
- .setIcon(android.R.drawable.ic_dialog_info)
- .setTitle("自定义的Builder:修改文本内容")
- .setView(customView) //添加自定义视图
- .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {//添加一个Button并注册其响应事件
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- EditText et_alteredText = (EditText)customView.findViewById(R.id.et_alteredText); //获取自定义View里面的EditText
- et_show.setText(et_alteredText.getText().toString());
- }
- })
- .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { //注意,这个是SetNegativeButton,上面那个是SetPositiveButton
- //不能重复名字,否则最后那个会覆盖前面那个,也就是说每个Buttion只显示一个。
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- dialog.cancel();
- }
- })
- .show();
b.布局文件alter_text.xml
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout
- android:id="@+id/widget1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- >
- <EditText
- android:id="@+id/et_alteredText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="18sp"
- android:layout_x="0px"
- android:layout_y="22px"
- >
- </EditText>
- </AbsoluteLayout>
4)嵌套的Builder
- final LayoutInflater layout2 = LayoutInflater.from(AlertDialog_Builder.this); //用于获取XML工厂,要使用final修饰
- final View customView2 = layout2.inflate(R.layout.alter_text, null); //创建自定义的View,要使用final修饰
- new AlertDialog.Builder(AlertDialog_Builder.this)
- .setIcon(android.R.drawable.ic_dialog_info)
- .setTitle("嵌套的Builder:显示文本内容")
- .setMessage(et_show.getText())
- .setPositiveButton(R.string.rename, new DialogInterface.OnClickListener() {//添加一个Button并注册其响应事件
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- new AlertDialog.Builder(AlertDialog_Builder.this)
- .setIcon(android.R.drawable.ic_dialog_info)
- .setTitle("嵌套的Builder:修改文本内容")
- .setView(customView2) //添加自定义视图
- .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {//添加一个Button并注册其响应事件
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- EditText et_alteredText = (EditText)customView2.findViewById(R.id.et_alteredText); //获取自定义View里面的EditText
- et_show.setText(et_alteredText.getText().toString());
- }
- })
- .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { //注意,这个是SetNegativeButton,上面那个是SetPositiveButton
- //不能重复名字,否则最后那个会覆盖前面那个,也就是说每个Buttion只显示一个。
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- dialog.cancel();
- }
- })
- .show();
- }
- })
- .setNegativeButton("取消", new DialogInterface.OnClickListener() { //注意,这个是SetNegativeButton,上面那个是SetPositiveButton
- //不能重复名字,否则最后那个会覆盖前面那个,也就是说每个Buttion只显示一个。
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- et_show.setText("");
- }
- })
- .show();
至于Builder的进一步使用,以后在补上。
- 顶
- 0
- 踩
Android之Builder对话框的一些常用方式的更多相关文章
- android 开发AlertDialog.builder对话框的实现
AndroidAPI提供了Dialog对话框控件,但google明确指出不建议开发者只是使用Dialog来创建对话框,而应该自定义对话框或者使用API中提供的Dialog的子类,如AlertDialo ...
- Android 自学之对话框
Android为我们提供了丰富的对话框支持,提供了四种常用的对话框: AlertDialog:功能丰富.实际应用最广泛的对话框. ProgressDialog:进度对话框,该对话框只用于简单的进度条封 ...
- Android中的几个基本常用控件
Android 中常用的几大UI控件 1. TextView : 用于在界面中显示一段文本信息 <TextView android:id="@+id/text_view" / ...
- android中跨进程通讯的4种方式
转自:http://blog.csdn.net/lyf_007217/article/details/8542359 帖子写的很好.看来一遍,试了一遍,感觉太有意义.必须转过来! android中跨进 ...
- Android UI系列--对话框(一)(AlertDialog,TimePickerDialog,DatePickerDialog,ProgressDialog)
一.Dialog介绍 dialog就是一个在屏幕上弹出一个可以让用户做出一个选择,或者输入额外的信息的对话框,一个对话框并不会沾满我们整个的屏幕,并且通常用于模型事件当中需要用户做出一个决定后才会继续 ...
- 【转】Android播放音频MediaPlayer的几种方式介绍
接下来笔者介绍一下Android中播放音频的几种方式,android.media包下面包含了Android开发中媒体类,当然笔者不会依次去介绍,下面介绍几个音频播放中常用的类: 1.使用MediaPl ...
- 【Android】Android 8种对话框(Dialog)
1.写在前面 Android提供了丰富的Dialog函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮).列表.单选.多选.等待.进度条.编辑.自定义等多种形式,将在第2部分介绍 ...
- 【Android】播放音频的几种方式介绍
接下来笔者介绍一下Android中播放音频的几种方式,android.media包下面包含了Android开发中媒体类,当然笔者不会依次去介绍,下面介绍几个音频播放中常用的类: 1.使用MediaPl ...
- Java Android 注解(Annotation) 及几个常用开源项目注解原理简析
不少开源库(ButterKnife.Retrofit.ActiveAndroid等等)都用到了注解的方式来简化代码提高开发效率. 本文简单介绍下 Annotation 示例.概念及作用.分类.自定义. ...
随机推荐
- 猴博士4小时讲完C语言视频教程
猴博士4小时讲完C语言视频教程,一共有9节课. 目录结构如下: 目录:/2020030-猴博士4小时讲完C语言 [1G] ┣━━1.C语言基本语句(上)(更多资源访问:www.jimeng365.cn ...
- Springmvc-crud-01错误
错误:无法显示图书列表内容 原因:获取存储域对象中的名字写错了 controller层: 前端页面: 解决方案:前后端的代码要保持一致(名字自己定义),写代码要细心 修改成功后的界面
- CentOS7.6配置ip
查看CentOS版本信息 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release (Core) 配置ip [root@loca ...
- VM虚拟机黑屏 鼠标进不去
#开始 可能是我脸黑吧 最近用虚拟机好几次都是黑屏 鼠标进不去 但是任务管理器显示确实有资源消耗 也就是说实际上应该是开机成功了(但是听不到声音 也许是没有开机吧) #解决方案 管理员权限打开 cmd ...
- BugReport-智慧农业APP
1.展示的界面显示不全 bug Description: 测试环境:win10.工具eclipse: 测试步骤:打开运行程序后模拟器启动,第一个界面显示过几秒跳到了另一个界面,问题是第一个界面显示不全 ...
- windows centos php-beast 安装
https://github.com/imaben/php-beast-binaries windows下 可以直接在这里下载dll 根据自己的php版本 还有是不是线程安全的 来选择下载对应的 放 ...
- 传奇身上装备升级系列脚本,以及UPGRADEITEMEX 脚本的详细参数解释
UPGRADEITEMEX 脚本的详细参数解释如下: UPGRADEITEMEX 物品位置(0-12) 属性位置(0-14) 成功机率(0-100) 点数机率(0-255) 是否破碎(0,1) 物品位 ...
- 「CF1042F」Leaf Sets
传送门 Luogu 解题思路 比较显然的一种做法: 我们把一个点的子树高度抠出来并排序记为 \(L_i\),找到最大的 \(i\) 使得 \(L_{i-1}+L_i\le K\). 于是我们把前 \( ...
- 基础_04_list and tuple
一.list(列表) list是Python里的一种容器,里面可以存储多个任何类型的数据,长度也可以任意伸缩,可以像C语言中数组那样,按照索引下标获取对应的值.但数组是一个存储多个固定类型变量的连续内 ...
- java 协程
协程是比线程更轻量级的程序处理单元,也可以说是运行在线程上的线程,由自己控制 1.适用于被阻塞的,且需要大量并发的场景. 2.不适用于,大量计算的多线程,遇到此种情况,更好实用线程去解决. 虽然Jav ...