Android popupwindow 失去焦点或者点击空白区域时消失的解决方法

先来看下Android API 的这个Methods:

public void setOutsideTouchable (boolean touchable)
Controls whether the pop-up will be informed of touch events outside of its window. This only makes sense for pop-ups that are touchable but not focusable, which means touches outside of the window will be delivered to the window behind. The default is false.

If the popup is showing, calling this method will take effect only the next time the popup is shown or through a manual call to one of the update() methods.

Parameters

touchabletrue if the popup should receive outside touch events, false otherwise


就是说,基本通过这个属性和setFocusable(true);就能实现点击别的区域让popup消失,

也能够这样做,设置点击popup窗口自身,让其消失,通过下边的方法即root重写onTouch方法:

 //点击窗口,PopupWindow消失           
                root.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        popup.dismiss();
                        return true;
                    }
                });

相同, 能够不用重写root的onTouch方法,而该重写 Activity的onTouchEvent()方法,正常情况下也能实现popup消失.


  @Override
    public boolean onTouchEvent(MotionEvent event) {  
        if (popup != null && popup.isShowing()) { 
            popup.dismiss(); 
            popup= null; 
        } 
        return super.onTouchEvent(event);
    }

可是, 我遇到一个问题,就是上述的方法都解决不了, 是什么原因? 

事实上,罪魁祸首就是, popup的代码顺序, 手贱先调用了 showAsDropDown()方法,在设置其它属性,导致了这样的情况.

showAsDropDown这种方法相当于 Dialog.show()方法,  假设是先show了, 然后其它属性即使是设置了,也起不到应有的作用. 

这一点,常常提醒别人,没想到到popup这里我犯了相同的错误.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值