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 ...
随机推荐
- OOP数据库操作方法
一.数据库操作 连接MYSQL数据 面向对象访问数据库e.g. 造对象 $dx=new MySQLi("localhost","root","123& ...
- javaScript初学者易错点
大家好,这是我在博客园写的第一篇博文.作为一名前端开发初学者,由于经验不足,水平有限,在做项目的过程中总会遇到这样或那样的问题,每每这时候,我都比较喜欢到博客园这里来寻求解决方案,结果也总是能找到满意 ...
- android下使用smack需引入的包
compile 'org.igniterealtime.smack:smack-android:4.2.0-alpha1' compile 'org.igniterealtime.smack:smac ...
- [原]武大预选赛F题-(裸并查集+下标离散化+floyd最短路)
Problem 1542 - F - Countries Time Limit: 1000MS Memory Limit: 65536KB Total Submit: 266 Accepted: 36 ...
- 编译android出错
注意:frameworks/base/nfc-extras/java/com/android/nfc_extras/NfcAdapterExtras.java 使用了未经检查或不安全的操作.注意:要了 ...
- 四大开源协议:BSD、Apache、GPL、LGPL
参考文献:http://www.fsf.org/licensing/licenses/ 现今存在的开源协议很多,而经过Open Source Initiative组织通过批准的开源协议目前有58种.我 ...
- 反编译android APK
我们经常会在如下的情况使用反编译 1.看到别人应用中的酷炫功能,想知道是如何实现的 2.别人应用的素材排版好漂亮,想套用模仿 百度一下就已经有一大堆反编译的教程了,我还是坚持学习记录一下. A ...
- 函数lock_rec_get_n_bits
/*********************************************************************//** Gets the number of bits i ...
- [转] 解析Qt资源文件使用
解析Qt资源文件使用 转自:http://mobile.51cto.com/symbian-270121.htm 本文详细的介绍了Qt文件的使用,和大部分GUI框架设计工具一样,Qt也引入了资源文件系 ...
- mysql 存储过程 事务; mysql的事务中包含一个存储过程
在asp.net结合mysql的开发中,我平时用到的事务处理是 使用 TransactionOptions 来进行处理 TransactionOptions transactionOption = ...