问题描述
我有一个弹出窗口,显示当我点击我的列表中的活动项目。问题是,后面键不关闭。我试图抓住我的列表中的活动返回键,但它并没有注册它......然后我试图注册一个onkeylistener为我传递给我的弹出式窗口的视图。像这样的:
I have a popup window displaying when I click an item in my list activity. The problem is that the back key doesn't close it. I tried catching the back key in my list activity but it doesn't register it...then I tried registering a onkeylistener to the view I'm passing to my popup window. Like this:
pop.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
boolean res=false;
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
Log.e("keydown","back");
if (pw.isShowing()) {
Log.e("keydown","pw showing");
pw.dismiss();
res = true;
}
} else {
res = false;
}
return res;
}
});
被传递到弹出这样的:
which is passed to a popup like this:
pw = new PopupWindow(
pop,
240,
70,
true);
但是,听者不火都不是。你能帮助我吗?我的想法:)
But that listener doesn't fire neither. Can you help me? I'm out of ideas :)
推荐答案
这是因为弹出窗口并没有onTouch或onKey事件作出反应,除非它有一个背景!= NULL。 检查出一些code我写以帮助这个。在基本情况下,你可以调用 PopupWindow#setBackgroundDrawable(新BitmapDrawable())
来迫使它采取行动,你期望的方式。你不会需要你自己onKey听众。您可能还需要调用 PopupWindow#setOutsideTouchable(真),如果你希望它消失,当用户在窗口边界的点击之外
。
This is because the popup window does not respond to onTouch or onKey events unless it has a background that != null. Check out some code I wrote to help with this. In the basic case you can to call PopupWindow#setBackgroundDrawable(new BitmapDrawable())
to force it to act the way you expect. You won't need your own onKey listener. You might also need to call PopupWindow#setOutsideTouchable(true)
if you want it to go away when the user clicks outside of the window boundaries.
扩展深奥的答案:
原因背景不能为空是因为在 PopupWindow#preparePopup
会发生什么。如果检测到背景!= NULL
它创建 PopupViewContainer
的调用实例和 setBackgroundDrawable
上,并把你的内容来看它。 PopupViewContainer
基本上是一个的FrameLayout
监听触摸事件和 KeyEvent.KEY code_BACK
事件关闭该窗口。如果背景== NULL,它不会做任何的,只是使用你的内容视图。你可以,作为替代根据 PopupWindow
来处理,延长你的根的ViewGroup
来表现你想要的方式
The reason the background cannot be null is because of what happens in PopupWindow#preparePopup
. If it detects background != null
it creates an instance of PopupViewContainer
and calls setBackgroundDrawable
on that and puts your content view in it. PopupViewContainer
is basically a FrameLayout
that listens for touch events and the KeyEvent.KEYCODE_BACK
event to dismiss the window. If background == null, it doesn't do any of that and just uses your content view. You can, as an alternative to depending on PopupWindow
to handle that, extend your root ViewGroup
to behave the way you want.
这篇关于Android的弹出窗口解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!