我看到了很多关于这个主题的帖子,但找不到适合我的答案。
我有一个 Activity ,当我弹出弹出窗口时,该 Activity 会变暗。 后退按钮可以工作,但只有在我第二次按下它时,第一次按下会关闭弹出窗口,但它不会取消 Activity 的变暗,因为我无法从弹出窗口捕获事件,第二次按下会被 Activity 捕获,只有这样我才能取消调暗它。
这是我为实现这一目标所做的尝试:
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();
}
}
最佳答案
改变
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();
}
}
关于android - 弹出窗口返回键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18310562/