Android 自定义Dialog,以及失去焦点之后,Dialog消失的解决
来源:互联网 发布:2017优化六上数学答案 编辑:程序博客网 时间:2024/04/28 13:19
自己参照网上的大量自定义Dialog的方法,也写出了在项目中使用的自定义Dialog,下面是实现代码:
/** * 自定义进度条Dialog * @author Administrator * */public class CustomProgressDialog extends Dialog {private static CustomProgressDialog mCustomProgressDialog = null;public CustomProgressDialog(Context context) {super(context);}public CustomProgressDialog(Context context, int theme) {super(context, theme);}public static CustomProgressDialog createDialog(Context context) {return createDialog(context, false);}public static CustomProgressDialog createDialog(Context context,boolean cancel) {mCustomProgressDialog = new CustomProgressDialog(context,R.style.CustomProgressDialog);mCustomProgressDialog.setContentView(R.layout.custom_progress_dialog);mCustomProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;ImageView loadingProfress = (ImageView) mCustomProgressDialog.findViewById(R.id.loading_progress);Animation animation = AnimationUtils.loadAnimation(context,R.anim.loading_progress_animation);loadingProfress.setAnimation(animation);mCustomProgressDialog.setCancelable(cancel);// 是否可以用“返回键”取消return mCustomProgressDialog;}@Overridepublic void onWindowFocusChanged(boolean hasFocus) {if (mCustomProgressDialog == null) {return;}if (!hasFocus) {dismiss();}}}这个是Dialog需要用到的布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" > <ImageView android:id="@+id/loading_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/loading_icon_rotation" /> <ImageView android:id="@+id/loading_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/loading_icon" /></RelativeLayout>还有进度条的动画效果:
<?xml version="1.0" encoding="utf-8"?><set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:duration="1600" android:fromDegrees="0" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" android:repeatMode="restart" android:startOffset="-1" android:toDegrees="+360" /></set>上面,已经把自定义Dialog实现的全部代码粘贴出来了,但是在使用过程中,出现了一个bug,在下面会进行详细说明。。。
----------------------------------
但是,上面的实现,在实际的使用过程,出现了这么一个情况,当你将顶部的状态栏下拉时,Dialog会消失,这是一个什么情况咧?
嘻嘻,因为在代码中,我重写了这么一个方法
@Overridepublic void onWindowFocusChanged(boolean hasFocus) {if (mCustomProgressDialog == null) {return;}if (!hasFocus) {dismiss();}}
onWindowFocusChanged(boolean hasFocus) :当Dialog获取焦点,或者失去焦点时,这个方法都会被调用,也就是当前界面焦点发生改变时,这个方法就会运行看一看看我重写的这个方法里面的实现,当失去焦点时,hasFocus == false , 会dismiss掉这个Dialog。
问题得到了解决,
因为重写了onWindowFocusChanged方法,里面的实现导致Dialog失去焦点后,dismiss掉。。。。我忘记了当时,为什么要重写这个方法了。。。
那么这个问题怎么解决,不用我说了吧。。。
//@Override//public void onWindowFocusChanged(boolean hasFocus) {//if (mCustomProgressDialog == null) {//return;//}//if (!hasFocus) {//dismiss();//}//}注释掉就行了。。。
行了,解释完毕。
代码天然无污染,可放心食用。。。。
0 0
- Android 自定义Dialog,以及失去焦点之后,Dialog消失的解决
- 关于Android中Dialog点击屏幕外失去焦点消失的问题
- Android自定义dialog并实现失去焦点,activity获取焦点的功能
- android 自定义dialog并实现失去焦点(背景透明)的功能
- Android自定义Dialog带Dialog的显示消失动画(一)
- 【Android】Dialog以及自定义Dialog
- Android中的Dialog的使用以及自定义Dialog
- Android 自定义Dialog以及参数的传递
- 自定义Dialog以及Dialog的相关问题
- android 自定义dialog弹出和消失动画
- android 自定义dialog弹出和消失动画
- android 自定义dialog弹出和消失动画
- android 自定义dialog弹出和消失动画
- 自定义Dialog android Dialog
- android Dialog的自定义
- 自定义Android的Dialog
- Android 自定义的Dialog
- android 自定义的dialog
- Ubuntu 的 基本使用方法
- c++primer 要点-变量和基本类型
- 阿里云api调用做简单的cmdb
- springMVC 3.1文档
- Windows如何打包Qt程序
- Android 自定义Dialog,以及失去焦点之后,Dialog消失的解决
- C# 编写Windows服务并设置为“允许服务与桌面交互”
- mongodb进阶二之mongodb聚合
- IOS开发网络篇之──ASIHTTPRequest详解
- python中sorted()函数的应用
- 【Java】一个数组实现三个栈 (未完待续)
- ubuntu安装php curl扩展
- 进程与线程的区别
- 【Python】Windows版本的Python开发环境的配置,Helloworld,Python中文问题,输入输出、条件、循环、数组、类