android之popupwindow点击空白消失

来源:互联网 发布:淘宝买身份证 编辑:程序博客网 时间:2024/04/30 14:16

查了查 大部分有两个方法

方法1:

protected void showPopupWindow() {//Context mContext = HahItemActivity.this;LayoutInflater mLayoutInflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);//保存转发等视图View xuanz_popunwindwow = mLayoutInflater.inflate(R.layout.popwindow_image, null);//转化为popunwindowmPopupWindow= new PopupWindow(xuanz_popunwindwow,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);mPopupWindow.setBackgroundDrawable(new BitmapDrawable());   mPopupWindow.setFocusable(true);mPopupWindow.setOutsideTouchable(true);mPopupWindow.update();//设置显示mPopupWindow.showAtLocation(findViewById(R.id.hahaimage_pop_layout), Gravity.CENTER, 0,0);

//设置显示mPopupWindow.showAtLocation(findViewById(R.id.hahaimage_pop_layout), Gravity.CENTER, 0,0);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());   //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!mPopupWindow.setFocusable(true);//!获取焦点!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1mPopupWindow.setOutsideTouchable(true);mPopupWindow.update();//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
上面这三个语句就可以控制啦点击别的地方消失啦,要写在ShowAtLocation之前.()设置popupWindow.setFocusable(true); 这样才能让popupWindow里面的布局控件获得点击的事件否则就被它的父亲view给拦截了。


方法2 :(这种方法可以处理popupwindows dimiss的时候一些其他的操作,比如让其他控件的隐藏,消失等):
代码如下popupWindow.setFocusable(false);//focusable要为false(不设置默认的就是False);
//这是Activity 的OnTouchEvent。OnTouchEvent代表的是Activity 获得事件(即为PopupWindow之外)
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return super.onTouchEvent(event);
}


注:亲测1可行

0 0
原创粉丝点击