popwindow 点击外部消失

自定义一个 PopupWindow,当使用的时候在构造方法中 传值进行初始值设置。 点击PopupWindow以外的部分,PopupWindow消失,this.setOutsideTouchable(true)属性就是点击外部消失,但是很多时候失效,,这就需要设置this.setBackgroundDrawable(new BitmapDrawable());  这样问题就解决了


package net.tycmc.zhinengwei.shebei.factory;



import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import org.androidannotations.annotations.ViewById;
import org.apache.commons.collections.MapUtils;


import net.tycmc.bulb.bases.xlistView.XListView;
import net.tycmc.zhinengwei.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

public class GongzuoshijianPopupWindow extends PopupWindow {
private View mMenuView;
private XListView linkmanListView;



private MyFactoryShebeiFragment context;



EditText et_starttime;// 开始时间


EditText et_endtime;// 结束时间
RelativeLayout relative_time_queding;// 工作时间 输入完成 确定按钮


RelativeLayout relative_time_buxian;// 工作时间 不限 按钮


public GongzuoshijianPopupWindow(MyFactoryShebeiFragment context) {


this.context = context;
LayoutInflater inflater = (LayoutInflater) context.getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.pop_gongzuoshijian, null);
et_starttime = (EditText) mMenuView.findViewById(R.id.et_starttime);
et_endtime = (EditText) mMenuView.findViewById(R.id.et_endtime);
relative_time_queding = (RelativeLayout) mMenuView
.findViewById(R.id.relative_time_queding);
relative_time_buxian = (RelativeLayout) mMenuView
.findViewById(R.id.relative_time_buxian);
relative_time_queding.setOnClickListener(context);
relative_time_buxian.setOnClickListener(context);
show();
}
 
public void show() {
// 设置SelectPicPopupWindow的View
this.setContentView(mMenuView);


this.setWidth(LayoutParams.MATCH_PARENT);
// 设置SelectPicPopupWindow弹出窗体的高
this.setHeight(LayoutParams.WRAP_CONTENT);
// 设置SelectPicPopupWindow弹出窗体可点击
this.setFocusable(true);
// 设置SelectPicPopupWindow弹出窗体动画效果
// this.setAnimationStyle(R.style.AnimBottom);
// 实例化一个ColorDrawable颜色为半透明
this.setOutsideTouchable(true);
this.setBackgroundDrawable(new BitmapDrawable());  

// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框



}




}
要实现 PopWindow点击事件,你可以按照以下步骤进行操作: 1. 首先,在你的代码中创建一个 PopWindow 对象,并设置它的内容和属性。 2. 在创建 PopWindow 对象后,使用 setTouchable(true) 方法来设置 PopWindow 可以接收点击事件。 3. 然后,调用 setOutsideTouchable(true) 方法来设置 PopWindow外部点击时可以被关闭。 4. 接下来,通过调用 setOnDismissListener() 方法来设置 PopWindow消失监听器。 5. 在监听器中,你可以实现你想要的点击事件逻辑。 下面是一个示例代码,演示了如何实现 PopWindow点击事件: ```java // 创建 PopWindow 对象 PopupWindow popupWindow = new PopupWindow(context); // 设置 PopWindow 的内容和属性 View contentView = LayoutInflater.from(context).inflate(R.layout.popup_content, null); popupWindow.setContentView(contentView); popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); // 设置 PopWindow 可以接收点击事件 popupWindow.setTouchable(true); // 设置 PopWindow外部点击时可以被关闭 popupWindow.setOutsideTouchable(true); // 设置 PopWindow消失监听器 popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { // 实现你想要的点击事件逻辑 // ... } }); // 显示 PopWindow popupWindow.showAsDropDown(anchorView); ``` 注意,这只是一个示例代码,你需要根据你的具体需求进行相应的修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值