我将把一个rootView传递给setContentView:
root = (FlyOutMenu) this.getLayoutInflater().inflate(R.layout.activity_myLayout, null);
setContentView(root);布局的定义如下:
<com.android.xyz.view.viewgroup.FlyOutMenu.FlyOutMenu
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/RelativeLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/dialog_label_topic" />
<EditText
android:id="@+id/dialog_searchbar_topic"
android:ems="10" >
</EditText>
<Button
android:id="@+id/dialog_menu_topic" />
<GridView
android:id="@+id/gv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/dialog_searchbar_topic"
android:layout_alignParentBottom="true" >
</GridView>
</RelativeLayout>如您所见,它是同一应用程序中不同包中的代码。
我的应用程序中有两个包:
1) com.android.xyz.view.viewgroup.FlyOutMenu 2) con.android.xyz
在MainActivity中的第二个包中,我重写了onKeyDown-方法,但它从未被调用。
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.e("onKeyUp","Fired");
if(keyCode == KeyEvent.KEYCODE_BACK) {
Log.e("fired with KeyEvent","true");
RelativeLayout.LayoutParams myLayoutParams = (android.widget.RelativeLayout.LayoutParams) gv.getLayoutParams();
myLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
gv.setLayoutParams(myLayoutParams);
}
return super.onKeyUp(keyCode, event);
}我没有附加任何onFocus侦听器。唯一请求焦点的视图是我活动中的搜索栏,但它也没有焦点侦听器。
我尝试了如下:
onKeyDown not always called in Android app
没有结果..。问题保持不变。
任何帮助都是非常感谢的。
有人知道为什么这不能被调用吗?
提前感谢
发布于 2015-06-19 01:32:00
希望这个答案对于你的问题还不算太晚;)
我在自己的自定义视图实现中遇到了同样的问题,我发现onKeyUp(int keyCode, KeyEvent event)是在View上调用的键盘处理事件。
然而,ViewGroup从不调用onKeyUp,而是调用自己的方法dispatchKeyEvent(KeyEvent event),这是您需要在自定义ViewGroup实现中进行@Override的方法。
如果别人遇到这个问题,希望这能帮上忙。
https://stackoverflow.com/questions/27382502
复制相似问题