Activity设置Dialog属性问题总结

来源:互联网 发布:吉利知豆电动小汽车 编辑:程序博客网 时间:2024/04/28 00:39

一、问题背景

  在开发过程中,我们有时会通过Activity实现弹窗效果,就是所谓的伪弹窗。实现很简单,AndroidMenifest.xml中将对应的Activity增加android:theme="@android:style/Theme.Dialog"属性即可(也可以自定义弹窗的样式)。对于弹窗,点击其周围的空白区域,正常情况下弹窗都会自动消失,然而,在2.x手机伪弹窗却不会自动消失。


二、问题分析

  根据问题现象,首先需要通过源码分析ActivityonTouchEvent()做什么处理。

  先分析api-11的源码如下:

 class Activity:public boolean onTouchEvent(MotionEvent event) {if (mWindow.shouldCloseOnTouch(this, event)) {finish();return true;}return false;}class Window:public boolean shouldCloseOnTouch(Context context, MotionEvent event) {if (mCloseOnTouchOutside && event.getAction() == MotionEvent.ACTION_DOWN&& isOutOfBounds(context, event) && peekDecorView() != null) {return true;}return false;}private boolean isOutOfBounds(Context 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 = getDecorView();return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))|| (y > (decorView.getHeight() + slop));

根据上面的源码,activity接收到touch事件时,会调用mWindow.shouldCloseOnTouch(),返回truefinish(),所以点击空白区域会关闭就是因为这里执行了finish()。在方法mWindow.shouldCloseOnTouch()中,会通过isOutOfBounds(context,event)判断是否点击了空白区域,另外还会判断变量mCloseOnTouchOutside是否为true,初始值是false,在Window中追查该变量,发现可以通过方法setCloseOnTouchOutside(booleanclose),修改该变量。在Activity中追查是否调用了mWindow.setCloseOnTouchOutside(),发现有这么一个方法:

    class Activity:   /**     * Sets whether this activity is finished when touched outside its window's     * bounds.     */    public void setFinishOnTouchOutside(boolean finish) {        mWindow.setCloseOnTouchOutside(finish);    }

注释解释得很清楚了,另外该方法在api11才新增的。实际上,我们并没有调用Activity.setFinishOnTouchOutside(),但依然可以点击空白区域消失(mCloseOnTouchOutside初始值是false),因此继续追查在哪个地方修改了mCloseOnTouchOutsidetrue。在类Activity中并没有自身调用到该接口setFinishOnTouchOutside(),接着重写了该接口并断点看看谁调用了,实际上也没有,因此就有可能是类Window自身调用并修改了mCloseOnTouchOutside。通过Activity.getWindow()发现对象mWindow其实是PhoneWindow以下的方法:

class PhoneWindow extends Window:protected ViewGroup generateLayout(DecorView decor) {…if (mAlwaysReadCloseOnTouchAttr || getContext().getApplicationInfo().targetSdkVersion>= android.os.Build.VERSION_CODES.HONEYCOMB) {    if (a.getBoolean( com.android.internal.R.styleable.Window_windowCloseOnTouchOutside, false)) {          setCloseOnTouchOutsideIfNotSet(true);     }}…}class Window:public void setCloseOnTouchOutsideIfNotSet(boolean close) {if (!mSetCloseOnTouchOutside) {     CloseOnTouchOutside = close;     mSetCloseOnTouchOutside = true;}}

通过上面的源码分析,已经可以清楚为何点击空白区域会消失的原因了,接着分析api-10的源码。

   class Activity:   public boolean onTouchEvent(MotionEvent event) {        return false;   }

所以,api-11以前的版本Activitytouch事件是不做任何处理的。

三、问题处理

针对该问题,如果点击Activity伪弹窗的空白区域需要关闭Activity,对于api11及以后的版本,只需要在Activity创建时调用setFinishOnTouchOutside(true)就可以了;对于之前的版本就需要重写方法onTouchEvent()了。对此有两者方案,一种就是直接采取api11源码的处理方式,如下:

     class Activity:     private boolean mIsFinishOnTouchOutside = true;     @Override     public boolean onTouchEvent(MotionEvent event) {        if (mIsFinishOnTouchOutside && event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(this, event)) {                finish();                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));    }        public void setFinishOnTouchOutside(boolean finish) {    mIsFinishOnTouchOutside = finish;    }

另一种就是在Activity执行onCreate()时,设置如下的窗口属性:

getWindow().setFlags(LayoutParams.FLAG_NOT_TOUCH_MODAL, LayoutParams.FLAG_NOT_TOUCH_MODAL);getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);

上面的属性表示点击伪弹窗外部空白区域时,不截取触摸事件,同时可以监听到点击了外部区域,即会回调到onTouchEvent(MotionEventevent),其中event是一个ACTION_OUTSIDE事件。因此,重写onTouchEvent()如下:

  @Override  public boolean onTouchEvent(MotionEvent event) {if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {finish();return true;}return super.onTouchEvent(event);}

然而,该处理方法有个问题就是,点击外部区域时,弹窗后面的控件也会接收到触摸事件。


0 0
原创粉丝点击