Android Cannot resolve symbol KeyEventCompat(android.support.v4.view.KeyEventCompat找不到)

升级后compileSdkVersion为26,因为此时classpath 'com.android.tools.build:gradle:3.0.0',v4包的版本是27.0.0。 报KeyEventCompat类(项目中用到KeyEventCompat的hasNoModifiers等方法来做的ViewPager的懒加载)找不到,回看25.3.1版的v4包中此类是存在的,经确认KeyEventCompat类在27.0.0版本中是不存在的,最后查看源码才知道hasNoModifiers方法已经被KeyEvent实现了。

贴出解决此处修改的用于实现ViewPager懒加载方法的部分代码

public boolean executeKeyEvent(KeyEvent event) {
    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            handled = arrowScroll(FOCUS_LEFT);
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            handled = arrowScroll(FOCUS_RIGHT);
            break;
        case KeyEvent.KEYCODE_TAB:
            if (Build.VERSION.SDK_INT >= 11) {
//报错地方
                // The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
                // before Android 3.0. Ignore the tab key on those devices.
//              if (KeyEventCompat.hasNoModifiers(event)) {
//                  handled = arrowScroll(FOCUS_FORWARD);
//              } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
//                  handled = arrowScroll(FOCUS_BACKWARD);
//              }

//正确代码
                if (event.hasNoModifiers()) {
                    handled = arrowScroll(FOCUS_FORWARD);
                } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
                    handled = arrowScroll(FOCUS_BACKWARD);
                }
            }
            break;
        }
    }
    return handled;
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/android-cannot-resolve-symbol-keyeventcompat-android-support-v4-view-keyeventcompat-not-found/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android Cannot resolve symbol KeyEventCompat(android.support.v4.view.KeyEventCompat找不到)
升级后compileSdkVersion为26,因为此时classpath 'com.android.tools.build:gradle:3.0.0',v4包的版本是27.0.0。 报KeyEventCompat类(项目中用到……
<<上一篇
下一篇>>
文章目录
关闭
目 录