【发布时间】:2012-02-06 09:21:16
【问题描述】:
我想为我的应用程序添加一个自定义视图。为此,我使用 WindowsManager:
final WindowManager wm = getWindowManager();
final View view = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.game_menu, null);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND | WindowManager.LayoutParams.FLAG_FULLSCREEN;
lp.dimAmount = (float) 0.6;
lp.format = PixelFormat.TRANSPARENT;
lp.windowAnimations = android.R.style.Animation_Dialog;
view.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
Log.d("12", "12");
if (keyCode == KeyEvent.KEYCODE_BACK)
{
wm.removeView(view); // This I need to hide my menu
}
return false;
}
});
wm.addView(view, lp); // I add menu like in Angry Birds and other games
但我无法捕获设备按键事件以隐藏此视图。
为什么在 WindowsManager 添加的视图中没有调用我的关键侦听器?我必须怎么做才能通过按下设备返回键来隐藏我的视图?
【问题讨论】:
标签: android events view window key