PopupWindow - 点击后退出但不在外面

时间:2015-12-09 15:28:41

标签: android popupwindow

我想点击后退按钮来关闭PopupWindow。但我也可以点击外面再做一件事,PopupWindow不要错过。 我尝试popupWindow.setFocusable(true);,但当我点击外面时,PopupWindow会解散。 我尝试自定义视图。

public class OtherBrifeIntroView extends LinearLayout
{
@Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
        {
            Toast.makeText(mContext, "test", Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

}

但它没有用。你可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

I solve it.Set TouchModal false,but the setTouchModal is hide,so I use reflect.

public static void setPopupWindowTouchModal(PopupWindow popupWindow, boolean touchModal)
    {
        if (null == popupWindow)
        {
            return;
        }
        Method method;
        try
        {
            method = PopupWindow.class.getDeclaredMethod("setTouchModal", boolean.class);
            method.setAccessible(true);
            method.invoke(popupWindow, touchModal);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

Finally.

setPopupWindowTouchModal(popupWindow, false);
popupWindow.setFocusable(true);

答案 1 :(得分:0)

您可以致电popupWindow.dismiss()手动关闭PopupWindow方法中的dispatchKeyEvent。当你在PopupWindow之外触摸时,为了阻止它被解雇,你可以尝试一些事情。

popupWindow.setOutsideTouchable(true);
popupWindow.setTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable()); 

玩上述值并看看结果是什么,我现在不在我的开发者计算机上,因此无法测试它是哪一个。

相关问题