方法一(这种方法可以处理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);
}
方法二:设置popupWindow参数(这种方法只能让自身消失,不能够提供其他伴随操作,比如让其他控件的隐藏,消失等)
pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(new BitmapDrawable());//关键代码
pop.setOutsideTouchable(true);
在售楼中的代码:
/**
* 显示待售和已售弹出框
*/
private void showPopWindow() {
int pWidthPx = Utils.dip2px(RoomInformationActivity.this, 100);//设置弹出框的宽度
int pHeight=Utils.dip2px(RoomInformationActivity.this, 40);//设置弹出框的高度
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vPopWindow = inflater.inflate(R.layout.item_popup_room, null,false);
tv_sale = (TextView) vPopWindow.findViewById(R.id.tv_sale);
showData();
popWindow = new PopupWindow(vPopWindow,pWidthPx, pHeight, true);
popWindow.setFocusable(true);
popWindow.setBackgroundDrawable(new BitmapDrawable());//重要代码
popWindow.setOutsideTouchable(true);
int[] location = new int[2];
fl_control.getLocationOnScreen(location);
popWindow.showAtLocation(fl_control, Gravity.NO_GRAVITY, location[0], location[1]-popWindow.getHeight());
/**
* 点击条目
*/
tv_sale.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if ("待售".equals(tv_sale.getText().toString().trim())) {
tv_pin_control.setText("待售");
}else {
tv_pin_control.setText("销控");
}
status=tv_pin_control.getText().toString().trim();
popWindow.dismiss();
}
});
}