android – 问题解雇弹出窗口

我实现了一个弹出菜单,只需点击一下按钮即可显示.这是我的onclick方法.

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;
    }
}

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

pw.setOutsideTouchable(true);

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

解决方法:

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

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.

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.

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

另一方面,click local变量应该做什么?它设置为true,因此无论何时调用showOverflow方法,它都会一直强制pw弹出,并且无论如何都会在以后将其设置为false,因为它的生命周期会在您离开该方法时结束.

您的代码应如下所示:

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作为参数.

上一篇:Javascript:关闭弹出窗口


下一篇:Javascript弹出窗口