PopUpWindow返回键事件

时间:2013-08-19 09:31:06

标签: android

我看到很多关于这个主题的帖子,但找不到合适的答案。

当我弹出弹出窗口时,我有一个变暗的活动。 后退按钮工作,但只有第二次我按它,第一次按下解除弹出窗口,但它不会使活动变暗,因为我无法从弹出窗口捕获事件,第二次按下是由活动抓住,然后我才能它没有暗淡。

这是我尝试完成此任务:

    m_PopupWindow.setBackgroundDrawable(new BitmapDrawable());
    m_PopupWindow.setOutsideTouchable(true);


    View popUpWindowLaout = m_PopupWindow.getContentView();
    popUpWindowLaout.setFocusableInTouchMode(true);


//first press doesnt get caught here
    popUpWindowLaout.setOnKeyListener(new View.OnKeyListener()
    {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if (keyCode == KeyEvent.KEYCODE_BACK)
            {
                m_ActionBar.show();
                unShadeTheActivity();
                m_PopupWindow.dismiss();
                return true;
            }
        }
    });

//这个func会抓住第二次按下并且可以正常工作,但是我希望第一次按下它会这样做。

@Override
public void onBackPressed() {
    if (m_PopupWindow != null)
    {
        m_ActionBar.show();
        unShadeTheActivity();
        m_PopupWindow.dismiss();
    }
    else
    {
        super.onBackPressed();
    }
}

2 个答案:

答案 0 :(得分:2)

更改

public void onBackPressed() {
    if (m_PopupWindow != null)
    {
        m_ActionBar.show();
        unShadeTheActivity();
        m_PopupWindow.dismiss();
    }
    else
    {
        super.onBackPressed();
    }
}

public void onBackPressed() {
  super.onBackPressed();
    if (m_PopupWindow != null)
    {
        m_ActionBar.show();
        unShadeTheActivity();
        m_PopupWindow.dismiss();
    }
else
{
// rest of the code
// you can use finish,dismiss or call startActivity
// finish();

}

}

答案 1 :(得分:2)

popupWindow.setOnShowListener(HandlePopupShowLister);
popupWindow.setOnDismissListener(HandlePopUpDismissListerner);

public static OnDismissListener HandlePopUpDismissListerner = new OnDismissListener() {

    @Override
    public void onDismiss(DialogInterface dialog) {
        // TODO Auto-generated method stub
        Log.i("HandlePopUpDismissListerner", "HandlePopUpDismissListerner");
        CommonVariable.IsPopupOpen = false;
    }
};
public static OnShowListener HandlePopupShowLister = new OnShowListener() {
 // onShowListener interface.
    @Override
    public void onShow(DialogInterface dialog) {
        Log.i("HandlePopupShowLister", "HandlePopupShowLister");
        // TODO Auto-generated method stub
        CommonVariable.IsPopupOpen = true;

    }
};