问题关闭弹出窗口 [英] Issue dismissing popup window

查看:24
本文介绍了问题关闭弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个弹出菜单,点击一个按钮就会显示出来.这是我的 onclick 方法.

I have a popup menu implemented , which shows up on click of a button. This is my onclick method.

public void showOverflow(View view) {

    boolean click = true;
    Button action = (Button) findViewById(R.id.btbAction);

    LayoutInflater inflater = (LayoutInflater) main.this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.overflow_layout, null);
    final PopupWindow pw = new PopupWindow(popupView,
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    pw.setOutsideTouchable(true);
    if (click) {
        pw.showAsDropDown(action, 0, 0);
        click = false;
    } else {
        pw.dismiss();
        click = true;
    }
}

单击按钮时会显示弹出窗口.现在,问题是当我在弹出窗口之外触摸时,窗口没有被关闭.我尝试将此属性设置为弹出窗口

The popup window shows up when the button is clicked. Now, the problem is that the window is not dismissed when i touch outside the popup window. I tried setting this property to the popup window

pw.setOutsideTouchable(true);

事情保持不变.请帮我解决这个问题

Things remain the same. Please help me fix this

推荐答案

您应该将 setOutsideTouchable 调用的参数更改为 true:pw.setOutsideTouchable(false);

You should change the setOutsideTouchable call's parameter to true: pw.setOutsideTouchable(false);

控制弹出窗口是否会被通知外部的触摸事件它的窗口.这仅对可触摸的弹出窗口有意义但不可聚焦,这意味着窗口外的触摸将是送到后面的窗户.默认为 false.

Controls whether the pop-up will be informed of touch events outside of its window. This only makes sense for pop-ups that are touchable but not focusable, which means touches outside of the window will be delivered to the window behind. The default is false.

如果弹出窗口正在显示,调用此方法只会生效下次显示弹出窗口时或通过手动调用其中一个update() 方法.

If the popup is showing, calling this method will take effect only the next time the popup is shown or through a manual call to one of the update() methods.

参数:touchable true 如果弹出窗口应该接收外部触摸事件,false 否则

Parameters: touchable true if the popup should receive outside touch events, false otherwise

另一方面,click 局部变量应该做什么?它设置为 true,所以它总是强制 pw 弹出,无论何时调用 showOverflow 方法,并且无缘无故稍后设置为 false,因为它的生命周期在您离开该方法时结束.

On the other hand, what is the click local variable supposed to do? It is set to true, so it will always force the pw to pop up, whenever the showOverflow method is called, and for no reason it is set to false later, because it's life cycle ends as you leave that method.

您的代码应如下所示:

private LayoutInflater inflater;
private Button action;
private PopupWindow pw;
private View popupView;
/*
 * (non-Javadoc)
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    popupView = inflater.inflate(R.layout.overflow_layout, null, false);

    action = (Button) findViewById(R.id.action);
    action.setOnClickListener(this);
}

public void showOverflow()
{
    pw = new PopupWindow(getApplicationContext());
    pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    pw.setOutsideTouchable(true);

    pw.setContentView(popupView);
    pw.showAsDropDown(action, 0, 0);
}

如果您在 Activity 类中,则应使用 getApplicationContext().否则你应该得到 Context 作为参数.

The getApplicationContext() shoud be used in case you are inside an Activity class. Otherwise you should get the Context as a parameter.

这篇关于问题关闭弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆