Activity使用Dialog样式导致点击空白处自动关闭的问题
16lz
2021-01-26
原文地址:http://blog.csdn.net/voiceofnet/article/details/9061309
将Activity设置成窗口的样式实现Dialog或者Popupwindow效果在开发中是很常用的一种方式,在AndroidMenifest.xml中将需要设置的Activity增加android:theme="@android:style/Theme.Dialog"属性即可。但是窗口化的Activity有个问题就是:点击窗口空白处Activity会finish。如何避免这个问题呢,办法如下:
一、如果API Level>=11有两种方式: 1、 res\values\styles.xml<resources> <style name="Theme.SoundRecorder" parent="@android:style/Theme.Holo.DialogWhenLarge"> <item name="android:windowCloseOnTouchOutside">false</item> </style> </resources>
2、
YourActivity.this.setFinishOnTouchOutside(false);
二、如果API Level<11
@Overridepublic boolean onTouchEvent(MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(this, event)) {return true;}return super.onTouchEvent(event);}private boolean isOutOfBounds(Activity context, MotionEvent event) {final int x = (int) event.getX();final int y = (int) event.getY();final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();final View decorView = context.getWindow().getDecorView();return (x < -slop) || (y < -slop)|| (x > (decorView.getWidth() + slop))|| (y > (decorView.getHeight() + slop));}
更多相关文章
- WindowManager和WindowManager.LayoutParams的使用以及实现悬浮
- RecyclerView实现Item点击事件方法二
- animation of android (1)
- Android使用内容提供者方式进行存储
- 安卓编译出错:jre\bin\java.exe'' finished with non-zero exit
- H5-input 弹起键盘遮盖输入框(Android),键盘顶不起来输入框, ios
- Android(安卓)混淆 以及 java代码方式实现混淆配置
- [Android]GC work way on Android(安卓)4.2.2
- ViewPager+Fragment实现TabHost