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懒加载方法的部分代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :