【问题标题】:onKeyDown isn't invokedonKeyDown 没有被调用
【发布时间】:2015-06-19 09:32:00
【问题描述】:

我将一个 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

没有结果...问题依旧。

感谢任何帮助。

有人知道为什么不能调用吗?

提前致谢

【问题讨论】:

    标签: android onkeydown


    【解决方案1】:

    希望这个答案对您的问题来说还不算太晚;)

    我在自己的自定义视图实现中遇到了同样的问题,我发现onKeyUp(int keyCode, KeyEvent event) 是在View 上调用的键盘处理事件

    但是ViewGroup 从不调用onKeyUp,而是调用自己的方法dispatchKeyEvent(KeyEvent event),这是您在自定义ViewGroup 实现中需要@Override 的方法。

    如果其他人遇到此问题,希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2019-03-31
      • 2012-12-16
      相关资源
      最近更新 更多