android 8.0 当View设置了onClickListener之后,FOCUSABLE标记会被默认设置上,这样导致isFocusable返回为true。View的setFlags有如下一段处理代码:
public void setOnClickListener(@Nullable OnClickListener l) {
if (!isClickable()) {
setClickable(true);
}
getListenerInfo().mOnClickListener = l;
}
public void setClickable(boolean clickable) {
setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
}
void setFlags(int flags, int mask) {
......(省略)
if (((mViewFlags & FOCUSABLE_AUTO) != 0) && (changed & (FOCUSABLE_MASK | CLICKABLE)) != 0) {
final int newFocus;
//设置onClicListener,CLICKABLE标记会被添加上,所以(mViewFlags & CLICKABLE) != 0是true,这样FOCUSABLE也会被设置上去。
if ((mViewFlags & CLICKABLE) != 0) {
newFocus = FOCUSABLE;
} else {
newFocus = NOT_FOCUSABLE;
}
}
......(省略)
}
FOCUSABLE_AUTO标记也是Android 8.0新增的,从源代码的注释中可以看出,此标记是默认设置到View中的。
/**
* This view determines focusability automatically. This is the default.
*/
public static final int FOCUSABLE_AUTO = 0x00000010;