事件 open/close 弹出窗口
event open/close popupWindow
我正在尝试创建一种带有 popupWindow 和自定义布局的 popupMenuItem。
我有一个按钮,当我点击它时会显示弹出窗口。当我再次单击此按钮或单击 popupWindow 外部时,我想触发和关闭此 popupWindow 事件。
但是目前不行,我的setTouchInterceptor没有触发,你有解决这个问题的办法吗?
每次打开此 popupWindow 时,即使使用 setOutsideTouchable(true),我也无法访问所有其他 UI 元素。
这是我的代码:
- popupMenuImageView 是我的下拉按钮(实际上是图像视图),它显示了我的弹出窗口
popupWindow 我的 PopupWindod 对象的全局变量。
popupMenuImageView = (ImageView) getView().findViewById(R.id.popup_menu_imageView);
popupMenuImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (popupWindow == null) {
int width = 375;
int height = 240;
TableLayout tableLayout = (TableLayout) getView().findViewById(R.id.custom_popup_menu_id);
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.custom_popup_menu, tableLayout);
popupWindow = new PopupWindow(layout, width, height, true);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
popupWindow.dismiss();
return true;
}
return false;
}
});
}
popupWindow.showAsDropDown(popupMenuImageView);
}
});
我也试过这个也没用:
popupMenuImageView = (ImageView) getView().findViewById(R.id.popup_menu_imageView);
createMenuItem();
popupMenuImageView.setOnClickListener(eventOpenCloseMenuItem);
View.OnClickListener eventOpenCloseMenuItem = new View.OnClickListener(){
@Override
public void onClick(View v) {
if (isShowing) {
popupWindow.showAsDropDown(popupMenuImageView);
isShowing = false;
}else{
popupWindow.dismiss();
}
}
};
private void createMenuItem(){
if (popupWindow == null) {
int width = 375;
int height = 240;
TableLayout tableLayout = (TableLayout) getView().findViewById(R.id.custom_popup_menu_id);
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.custom_popup_menu, tableLayout);
popupWindow = new PopupWindow(layout, width, height, true);
popupWindow.setOutsideTouchable(false);
popupWindow.setFocusable(true);
}
我认为您需要 setBackgroundDrawable()
在显示弹出窗口之前,以便它处理触摸事件:
popupWindow.setBackgroundDrawable(new ColorDrawable());
如果您只想通过在弹出窗口外单击来关闭弹出窗口 window,则不需要设置拦截器。只需将 background drawable 设置为 null 例如
popupWindow.setBackgroundDrawable (new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
并删除:
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
popupWindow.dismiss();
return true;
}
return false;
}
});
请在此处查看我对类似问题的回答PopupWindow - Dismiss when clicked outside
我正在尝试创建一种带有 popupWindow 和自定义布局的 popupMenuItem。
我有一个按钮,当我点击它时会显示弹出窗口。当我再次单击此按钮或单击 popupWindow 外部时,我想触发和关闭此 popupWindow 事件。
但是目前不行,我的setTouchInterceptor没有触发,你有解决这个问题的办法吗?
每次打开此 popupWindow 时,即使使用 setOutsideTouchable(true),我也无法访问所有其他 UI 元素。
这是我的代码:
- popupMenuImageView 是我的下拉按钮(实际上是图像视图),它显示了我的弹出窗口
popupWindow 我的 PopupWindod 对象的全局变量。
popupMenuImageView = (ImageView) getView().findViewById(R.id.popup_menu_imageView); popupMenuImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (popupWindow == null) { int width = 375; int height = 240; TableLayout tableLayout = (TableLayout) getView().findViewById(R.id.custom_popup_menu_id); LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = layoutInflater.inflate(R.layout.custom_popup_menu, tableLayout); popupWindow = new PopupWindow(layout, width, height, true); popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_OUTSIDE){ popupWindow.dismiss(); return true; } return false; } }); } popupWindow.showAsDropDown(popupMenuImageView); } });
我也试过这个也没用:
popupMenuImageView = (ImageView) getView().findViewById(R.id.popup_menu_imageView);
createMenuItem();
popupMenuImageView.setOnClickListener(eventOpenCloseMenuItem);
View.OnClickListener eventOpenCloseMenuItem = new View.OnClickListener(){
@Override
public void onClick(View v) {
if (isShowing) {
popupWindow.showAsDropDown(popupMenuImageView);
isShowing = false;
}else{
popupWindow.dismiss();
}
}
};
private void createMenuItem(){
if (popupWindow == null) { int width = 375; int height = 240; TableLayout tableLayout = (TableLayout) getView().findViewById(R.id.custom_popup_menu_id); LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = layoutInflater.inflate(R.layout.custom_popup_menu, tableLayout); popupWindow = new PopupWindow(layout, width, height, true); popupWindow.setOutsideTouchable(false); popupWindow.setFocusable(true); }
我认为您需要 setBackgroundDrawable()
在显示弹出窗口之前,以便它处理触摸事件:
popupWindow.setBackgroundDrawable(new ColorDrawable());
如果您只想通过在弹出窗口外单击来关闭弹出窗口 window,则不需要设置拦截器。只需将 background drawable 设置为 null 例如
popupWindow.setBackgroundDrawable (new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
并删除:
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
popupWindow.dismiss();
return true;
}
return false;
}
});
请在此处查看我对类似问题的回答PopupWindow - Dismiss when clicked outside