Android PopupWindow可防止在锚点击时发生错误

时间:2013-10-21 13:57:20

标签: android toggle popupwindow dismiss

我有一个popupWindow在按钮点击时showAsDropDown,这个popupWindow有setOutsideTouchable(true),我想在我单击我的按钮时切换显示弹出窗口,但是当我点击弹出窗口外时也要关闭。问题是在按钮点击之前调用外部触摸,然后我的弹出窗口隐藏并显示。 有没有办法做到这一点?

固定 !!!

我发现问题是我的popupwindow没有集中注意力而且我必须打电话

popupWindow.setFocusable(true);

修复它。

1 个答案:

答案 0 :(得分:0)

Please check this out.

如果我以正确的方式理解,

// Pop up Window showing

    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
            .getSystemService(LAYOUT_INFLATER_SERVICE);
    popupView = layoutInflater.inflate(R.layout.menu_popup, null);
    popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    popupWindow.setOutsideTouchable(true);

 // Add setBackgroundDrawable to the pop up window to close the dialog :

        popupWindow.setBackgroundDrawable(getResources().getDrawable(
            android.R.color.transparent));
    popupWindow.setTouchable(true);

使用以下检查,您可以根据需要显示弹出窗口

findViewById(R.id.topMenu).setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                        popupWindow
                                .showAsDropDown(findViewById(R.id.topMenu));

            });

我检查过这个。它工作正常。