zoukankan      html  css  js  c++  java
  • Activity使用Dialog样式导致点击空白处自动关闭的问题

    将Activity设置成窗口的样式实现Dialog或者Popupwindow效果在开发中是很常用的一种方式,在AndroidMenifest.xml中将需要设置的Activity增加android:theme="@android:style/Theme.Dialog"属性即可。但是窗口化的Activity有个问题就是:点击窗口空白处Activity会finish。如何避免这个问题呢,办法如下:

    一、如果API Level>=11有两种方式:
    1、
    resvaluesstyles.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

    @Override
        public 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));
        }
  • 相关阅读:
    bootstrap模版
    spark
    断点
    如何让数据动起来?Python动态图表制作一览。
    证据就在代码里
    windows下oracle的ora-27100错误
    SQL优化 | MySQL问题处理案例分享三则
    MySQL安装好之后本地可以连接,远程连接卡死
    MySQL千万级大表在线变更表结构
    ORA-39006错误原因及解决办法
  • 原文地址:https://www.cnblogs.com/longhs/p/4056918.html
Copyright © 2011-2022 走看看