Android学习笔记-Dialog详解
1.对话框的使用
1.1AlertDialog的显示
简单对话框以及监听的设置:重点掌握三个按钮(也就是三上单词):
PositiveButton(确认按钮);NeutralButton(忽略按钮)
|
AlertDialog.Builder bud1=new Builder(mContext); bud1.setTitle("提示信息"); bud1.setMessage("您的信息已提交完成!"); bud1.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); //对话框消失 } }); bud1.setPositiveButton(" 取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel();//取消对话框 Toast.makeText(mContext, "你已取消会话!", Toast.LENGTH_LONG).show(); } }); bud1.setNeutralButton("忽略", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Code06_03.this, "你已忽略。。。", Toast.LENGTH_LONG).show(); } }); //单选按钮,选取其中一个 //1.设置数据源 final String[] items = {"android", "ipone", "Symbian"}; //2.数据适配 bud1.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(mContext,items[which],Toast.LENGTH_SHORT).show(); } }); //自定义对话框 ImageView img = new ImageView(this); img.setImageResource(R.drawable.icon); Bud1 .setView(img) bud1.create().show();//显示对话框 |
1.2.ProgressDialog的显示
关键为什么在子线程中调用UI操作未报错问题,经过分析,
ProgressDialg内部已经实现了Hanlder,因而未报错。而runOnUiThread的使用也显的多余了。
|
protected void dialog9() { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5 * 1000); // progressDialog.dismiss(); runOnUiThread(finishDialog); } catch (InterruptedException e) { } } }).start(); progressDialog = ProgressDialog.show(Code06_07.this, "请稍等", "数据正在加载中...", true); } private Runnable finishDialog = new Runnable() { @Override public void run() { progressDialog.dismiss(); } }; |
|
m_pDialog = new ProgressDialog(Code06_09.this); // 设置进度条风格,风格为长形 m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 设置ProgressDialog 标题 m_pDialog.setTitle("提示"); // 设置ProgressDialog 提示信息 m_pDialog.setMessage("这是一个长形对话框进度条"); // 设置ProgressDialog 标题图标 m_pDialog.setIcon(R.drawable.icon); // 设置ProgressDialog 进度条进度 m_pDialog.setProgress(100); // 设置ProgressDialog 的进度条是否不明确 m_pDialog.setIndeterminate(false); // 设置ProgressDialog 是否可以按退回按键取消 m_pDialog.setCancelable(true); // 让ProgressDialog显示 m_pDialog.show(); new Thread() { public void run() { try { while (m_count <= 100) { // 由线程来控制进度。 m_pDialog.setProgress(m_count++); Thread.sleep(1000); } m_pDialog.cancel(); } catch (InterruptedException e) { m_pDialog.cancel(); } } }.start(); } |
我在测试代码时,遇到一个过时的方法,有一个新的API以供使用
/***
* 第一个参数是用来确定哪个按钮绑定监听
* @param whichButton Which button to set the listener on, can be one of
* 有以下常量来提供选择
* BUTTON_POSITIVE
* {@link DialogInterface#BUTTON_POSITIVE}
* {@link DialogInterface#BUTTON_NEGATIVE}
* {@link DialogInterface#BUTTON_NEUTRAL
**/
progress.setButton(AlertDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
// 点击“确定按钮”取消对话框
dialog.cancel();
}
});
经以上分析,与代码的编写,可以总结出:ProgressDialog的延时,使用了Handler机制,可以放心的在Thread中延时,在Thread中的操作有 setProgress,dismiss等方法。
1.3 自定义对话框-
LayoutInflater inflater=mContext.getLayoutInflater();
mView=inflater.inflate(R.layout.dialog,// 自定义对话框视图
null);
AlertDialog.Builder(mContext).setView(mView);
飞哥今天讲的知识点,就是关于两个类的使用。ProgressDialog,AlertDialog这两个类,主要讲解了如何使用它们的方法,以及ProgressDialog在特性,在Thread中使用。
1.4AlertDialog解析
AlertDialog extens Dialog implents DialogInterface
三个子类:DataPicker,ProgressDialog,TimePickerDialog
接下来,看看APIGuida有什么惊喜呢?
A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
通过这句话,我知道了飞哥的不易,把AlertDialog的功能全用了一遍,the,I Knowed it,and did it
DatePickerDialog or TimePickerDialog
A dialog with a pre-defined UI that allows the user to select a date or time.
一个提前定义好的对话框,允许用户选择时间或日期
you should use a DialogFragment as a container for your dialog. The DialogFragment class provides all the controls you need to create your dialog and manage its appearance, instead of calling methods on the Dialog object
看到这句话,不知道你有何感想-感觉前面的代码没用了有木有,大Boss是这个DialogFragment呀!不要急,请看后面。
简单的说:1.正确的处理Dialog的生命周期
2.对话框的简单利用,就像Fragment一样,当成一个组件
3.支持API级别泛围广Android1.6以上均支持,当然需要支持库
这里Google给出一个示例,封装AlertDialog,以复用
|
public class FireMissilesDialogFragment extends DialogFragment { |
当你获取实例后,便可以show了
参考:
FragmentDialog:详解:传送门
Andorid SDK Dialog
扩展阅读:
Full Screen DialogFragment (over ActionBar) in Android:传送门
Android学习笔记-Dialog详解的更多相关文章
- Angular6 学习笔记——路由详解
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
- Angular6 学习笔记——组件详解之组件通讯
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
- Angular6 学习笔记——组件详解之模板语法
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
- JavaScript学习笔记-实例详解-类(二)
实例详解-类(二) //===给Object.prototype添加只读\不可枚举\不可配置的属性objectId(function(){ Object.defineProperty(Object ...
- JavaScript学习笔记-实例详解-类(一)
实例详解-类(一): //每个javascript函数(除了bind())都自动拥有一个prototype对象// 在未添加属性或重写prototype对象之前,它只包含唯一一个不可枚举属性const ...
- Android学习——uses-sdk标签详解
1 前言 我们都知道,Android的版本在不断的迭代,并且每个版本都加入了不同的新特性.那么随着Android的用户量越来越多,Android的开发人员就必须熟悉Android各个版本的特性并且确保 ...
- Android学习之 Intent详解
一. Intent 作用 Intent 是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由Intent来协助完成android各个组件之间的通讯.比如说调用startActivity()来 ...
- android 开发 对话框Dialog详解
转载请注明出处:红亮的专栏:http://blog.csdn.net/liang5630/article/details/44098899 Android中的对话框形式大致可分为五种:分别是一般对话框 ...
- android学习——GestureDetector.OnGestureListener 详解
Android Touch Screen 与传统Click Touch Screen不同,会有一些手势(Gesture),例如Fling,Scroll等等.这些Gesture会使用户体验大大提升.An ...
随机推荐
- 分析和解析PHP代码的7大工具
PHP已成为时下最热门的编程语言之一,然而却有许多PHP程序员苦恼找不到合适的工具来帮助自己分析和解析PHP代码.今天小编就为大家介绍几个非常不错的工具,来帮助程序员们提高自己的工作效率,一起来看看吧 ...
- Fedora 15 KDE中如何打开software management及如何应用
Fedora 15 KDE中如何打开software management级如何应用 software management中有转载和卸载软件(Get and remove software)的功能 ...
- Unix/Linux 关机命令
关机命令 AIX shutdown now Solaris init 5 Redhat shutdown now Centos shutdown now
- wget 批量下载目录文件
wget -r -p -k -np http://源目录 ./本地目标目录
- [HIHO1223]不等式(离散化,枚举)
题目链接:http://hihocoder.com/problemset/problem/1223 这题不难,难点在于小数的处理.可以0.5为步长枚举,也可以扩大偶数倍枚举. /* ━━━━━┒ギリギ ...
- leetcode:Power of Two
Given an integer, write a function to determine if it is a power of two. 分析:这道题让我们判断一个数是否为2的次方数(而且要求 ...
- 【Python】如何安装easy_install?
[Python]如何安装easy_install? http://jingyan.baidu.com/article/b907e627e78fe146e7891c25.html easy_instal ...
- hibernate的save()和persit()之间的区别
这个问题啊,我在传智的Hibernate 视频上有小段讲解,save() 和persist() 都是持久化的保存,这两个方法在已经开启事物的情况下没多大区别:在不开启事物的时候save()方法会把数据 ...
- linux系统下怎么安装.deb文件
linux系统下怎么安装.deb文件? deb 是 ubuntu .debian 的格式.rpm 是 redhat .fedora .suse 的格式. 他们不通用(虽然可以转换一下). deb是de ...
- Flume学习 & Kafka & Storm 等 & Log4J 配置
正在学习这篇文章: http://blog.csdn.net/ymh198816/article/details/51998085 和工作中接触的电商.订单.分析,可以结合起来. 开宗明义,这幅图片: ...