Android点击popupwindow以外区域 popupwindow自动消失(转载)

方法一(这种方法可以处理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);

如果支持我的话,请点击左上方为我投票。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,如果你想要点PopupWindow周围关闭弹窗,可以通过以下几个步骤实现: 1.创建一个透明的背景层,并添加点事件,当用户点背景层时,关闭PopupWindow。 2.在PopupWindow的showAsDropDown()方法中,设置setBackgroundDrawable(),将PopupWindow的背景设置为透明,这样点背景层时,点事件才能被响应。 下面是示例代码: ```java // 创建透明的背景层 View backgroundView = new View(context); backgroundView.setBackgroundColor(Color.parseColor("#80000000")); backgroundView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); // 创建PopupWindow View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setFocusable(true); // 设置PopupWindow可以获取焦点 popupWindow.setOutsideTouchable(true); // 设置PopupWindow外部可点 popupWindow.showAsDropDown(anchorView); // 添加背景层 ViewGroup rootView = (ViewGroup) ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content); rootView.addView(backgroundView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); ``` 这样,当用户点PopupWindow周围的背景层时,就可以关闭弹窗了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值