Android popupwindow 失去焦点或者点击空白区域时消失的解决方法
2014-04-26 02:28
615 查看
先来看下Android API 的这个Methods:
public void setOutsideTouchable
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
Parameters
See Also
就是说,基本通过这个属性和setFocusable(true);就能实现点击别的区域让popup消失,
也可以这样做,设置点击popup窗体自身,让其消失,通过下边的方法即root重写onTouch方法:
同样, 可以不用重写root的onTouch方法,而该重写 Activity的onTouchEvent()方法,正常情况下也能实现popup消失.
但是, 我遇到一个问题,就是上述的方法都解决不了, 是什么原因?
其实,罪魁祸首就是, popup的代码顺序, 手贱先调用了 showAsDropDown()方法,在设置其他属性,导致了这种情况.
showAsDropDown这个方法相当于 Dialog.show()方法, 如果是先show了, 然后其他属性即使是设置了,也起不到应有的作用.
这一点,经常提醒别人,没想到到popup这里我犯了同样的错误.
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 isfalse.
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
touchable | true if the popup should receive outside touch events, false otherwise |
---|
isOutsideTouchable()
isShowing()
update()
就是说,基本通过这个属性和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这里我犯了同样的错误.
相关文章推荐
- Android popupwindow 失去焦点或者点击空白区域时消失的解决方法
- Android popupwindow 失去焦点或者点击空白区域时消失的解决方法
- Android中popupWindow点击外面区域以及返回键,使popupWindow消失的方法
- Android 弹窗,失去焦点(点击空白处)消失的方法
- android PopupWindow点击外部和返回键消失的解决方法
- 解决点击空白关闭popupWindow方法过时问题
- android:点击popupwindow以外区域 popupwindow自动消失(转载)
- Android 点击空白区域 软键盘消失
- popup window点击窗口外区域不消失的解决方法
- Android 当EditextText失去焦点,或者点击Activity的时候关闭软键盘或者其他布局
- android:点击popupwindow以外区域 popupwindow自动消失(转载)
- Android PopupWindow 点击消失解决办法
- CentOS 7运维管理笔记(12)----PHP页面失去焦点后变成空白的解决方法
- ToolStrip失去焦点时,点击第二次才执行事件的解决方法
- android:点击popupwindow以外区域 popupwindow自动消失
- 删除web文本框中的内容需要或者文本框失去焦点,点击“Backspace”键时页面回退,屏蔽页面回退键的方法
- Android点击popupwindow以外区域 popupwindow自动消失(转载)
- 解决Android按住空白区域,导致点击其他按钮无法触发点击事件的问题
- android EditText 点击空白失去焦点
- Android下关于AlertDialog点击 确定或者取消按钮 对话框自动消失的 解决办法