2

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

当我弹出弹出窗口时,我有一个变暗的活动。后退按钮有效,但仅在我第二次按下时,第一次按下关闭弹出窗口,但它不会取消活动,因为我无法从 popupwindows 捕获事件,第二次按下被活动捕获,只有这样我才能取消调暗它。

这是我的尝试:

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

//这个函数将捕捉第二次按下并且会工作,但我希望第一次按下会这样做。

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

2 回答 2

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();

}

}
于 2013-08-19T09:35:42.010 回答
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;

    }
};
于 2013-08-19T09:38:22.997 回答