当按下菜单键时,不会触发升级到AppCompat v22.1.0以及现在的onKeyDown和onKeyUp

时间:2015-04-24 16:04:45

标签: android android-support-library onkeydown onkeyup

我刚刚升级了我的应用以使用新发布的v22.1.0 AppCompat,现在onKeyDown并且在按下菜单键时未触发onKeyUp。其他键正确触发onKeyDownonKeyUp,但是当我按下菜单键时没有任何反应。如果我降级到v22.0.0,一切都恢复正常。

我该如何解决?

2 个答案:

答案 0 :(得分:31)

8月23日更新

这个has been fixed在appcompat-v7支持库的v23.0.0中再次出现。更新到上一个版本以查看此修复。

7月19日更新

不幸的是,AppCompat v22.2.1打破了onKeyDownonKeyUp个事件again。我刚刚更新AppCompatActivityMenuKeyInterceptor以支持v22.1.x和v22.2.1

5月29日更新

appcompat-v7支持库的v22.2.0中的has been fixed。更新到上一个版本以查看此修复。

不幸的是,AppCompat v22.1.0拦截onKeyDownonKeyUp事件,并且在按下菜单键时不会传播它们。唯一可能的解决方案是在AppCompat之前使用Reflection来拦截onKeyDownonKeyUp事件。

将此课程添加到您的项目中:

public class AppCompatActivityMenuKeyInterceptor {

    private static final String FIELD_NAME_DELEGATE = "mDelegate";
    private static final String FIELD_NAME_WINDOW = "mWindow";

    public static void intercept(AppCompatActivity appCompatActivity) {
        new AppCompatActivityMenuKeyInterceptor(appCompatActivity);
    }

    private AppCompatActivityMenuKeyInterceptor(AppCompatActivity activity) {
        try {
            Field mDelegateField = AppCompatActivity.class.getDeclaredField(FIELD_NAME_DELEGATE);
            mDelegateField.setAccessible(true);
            Object mDelegate = mDelegateField.get(activity);

            Class mDelegateClass = mDelegate.getClass().getSuperclass();
            Field mWindowField = null;

            while (mDelegateClass != null) {
                try {
                    mWindowField = mDelegateClass.getDeclaredField(FIELD_NAME_WINDOW);
                    break;
                } catch (NoSuchFieldException ignored) {
                }

                mDelegateClass = mDelegateClass.getSuperclass();
            }

            if (mWindowField == null)
                throw new NoSuchFieldException(FIELD_NAME_WINDOW);

            mWindowField.setAccessible(true);
            Window mWindow = (Window) mWindowField.get(mDelegate);

            Window.Callback mOriginalWindowCallback = mWindow.getCallback();
            mWindow.setCallback(new AppCompatWindowCallbackCustom(mOriginalWindowCallback, activity));
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    private class AppCompatWindowCallbackCustom extends WindowCallbackWrapper {

        private WeakReference<AppCompatActivity> mActivityWeak;

        public AppCompatWindowCallbackCustom(Window.Callback wrapped, AppCompatActivity appCompatActivity) {
            super(wrapped);

            mActivityWeak = new WeakReference<AppCompatActivity>(appCompatActivity);
        }

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            final int keyCode = event.getKeyCode();

            AppCompatActivity appCompatActivity = mActivityWeak.get();

            if (appCompatActivity != null && keyCode == KeyEvent.KEYCODE_MENU) {
                if (appCompatActivity.dispatchKeyEvent(event))
                    return true;
            }

            return super.dispatchKeyEvent(event);
        }
    }
}

在您活动的AppCompatActivityMenuKeyInterceptor.intercept(this)中致电onCreate

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Initialize the interceptor
        AppCompatActivityMenuKeyInterceptor.intercept(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Now onKeyDown is called also for KEYCODE_MENU
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            //do your stuff

            //return false if you want to propagate the
            //KeyEvent to AppCompat, return true otherwise
            return false;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // Now onKeyUp is called also for KEYCODE_MENU
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            //do your stuff

            //return false if you want to propagate the
            //KeyEvent to AppCompat, return true otherwise
            return false;
        }

        return super.onKeyUp(keyCode, event);
    }
}

如果您使用ProGuard或DexGuard,请将这些规则添加到您的配置中:

-keepclassmembers class android.support.v7.app.AppCompatActivity {
    private android.support.v7.app.AppCompatDelegate mDelegate;
}

-keepclassmembers class android.support.v7.app.AppCompatDelegateImplBase {
    final android.view.Window mWindow;
}

现在,您的活动也可以为菜单键收到onKeyDownonKeyUp事件。

答案 1 :(得分:1)

可以简单地使用onKeyUp()代替onKeyDown()dispatchKeyEvent()。请查看android-developers.blogspot.com中的以下代码。

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getRepeatCount() == 0) {

            // Tell the framework to start tracking this event.
            getKeyDispatcherState().startTracking(event, this);
            return true;

        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            getKeyDispatcherState().handleUpEvent(event);
            if (event.isTracking() && !event.isCanceled()) {
                // DO BACK ACTION HERE
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    } else {
        return super.dispatchKeyEvent(event);
    }
}