您的位置:首页 > 运维架构

PopupWindow

2013-10-09 11:11 405 查看
1. PopupWindow点击外部消失的两种方式

(1) 仅仅点击外部消失, 没有其他操作

popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new PaintDrawable(android.R.color.transparent));


(2) 点击外部, 有其他操作

popupWindow.setFocusable(true);
// popupWindow.setBackgroundDrawable(new PaintDrawable(android.R.color.transparent));
popupWindow.getContentView().setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// 1. PopupWindow位于Activity顶部时
if (event.getY() > popupWindowHeight) { // 当PopupWindow位于Activity顶部时, 点击外部就是当点击位置的Y坐标大于PopupWindow的高度时;
dismissCover(popupWindow); // 点击外部的操作
}
// 2. PopupWindow位于Activity底部时
//if (event.getY() <= 0) {  // 当PopupWindow位于Activity底部时, 点击的Y坐标为负值
//	dismissCover(popupWindow); // 点击外部的操作
//}
// 3. PopupWindow位于Activity中任意位置时, 需要获取View的位置(参考http://blog.csdn.net/wo334499/article/details/11650393), 当点击的坐标不在范围内, 执行相应操作
return false;
}
});
// 当设置onTouch监听, 点击外部消失时, 返回键会失效, 需要添加如下代码
popupWindow.getContentView().setFocusableInTouchMode(true); // 必须加这句才能返回键退出
popupWindow.getContentView().setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.e("zz", "onKeyonKeyonKeyonKeyonKeyonKey");
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (ivCover.getVisibility() == View.VISIBLE) {
dismissCover(popup);
return true;
}
}
return false;
}
});


2. PopupWindow中存在EditText
(1) 必须添加popupWindow.setFocusable(true);才能弹出软键盘

(2) 当点击过EditText后, 点击返回键将不再有效. 

解决方法:  需要为EditText也添加上OnkeyListener才会有效. 总的来说, 可以写一个内部类, 创建一个对象,然后为PopupWindow的ContentView和EditText分别设置监听.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: