Activity类通过布局id找到布局文件后,对各个组件进行布局,其中就包括View的创建。
View的创建会通过方法 public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)
传递的参数:
1. 其中后面两个参数,在View类的内部都是传了0;
2. attrs则是View的属性集;
3. context是调用这个View的上下文,这里就理解为创建布局文件的Activity类吧。
View类中,通过 mSourceLayoutId = Resources.getAttributeSetSourceResId(attrs);
获取到View所属布局的id;
再通过
final TypedArray a = context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.View, defStyleAttr, defStyleRes);
将attrs中的属性值存进数组a中。这为后面轮询属性值做准备
接下来会通过轮询的方式查看a中有哪些属性,格局对应的属性来进行相对应的方法,如下的是onClick,如果在布局文件中设计了onCLick属性,这会走到下面这个位置,通过attr属性找到对应的属性值,然后利用这个属性值(方法名)去寻找方法
final int N = a.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
switch (attr) {
...
case R.styleable.View_onClick:
if (context.isRestricted()) {
throw new IllegalStateException("The android:onClick attribute cannot " + "be used within a restricted context");
}
final String handlerName = a.getString(attr);
if (handlerName != null) {
setOnClickListener(new DeclaredOnClickListener(this, handlerName));
}
break;
...
}
}
a中的index跟View的属性值是一一对应的,但轮询到onClick属性值后,会通过方法
setOnClickListener(new DeclaredOnClickListener(this, handlerName))
来设置监听点击的方法名。
下面的hostView其实就是View本身
public DeclaredOnClickListener(@NonNull View hostView, @NonNull String methodName) {
mHostView = hostView;
mMethodName = methodName;
}
@Override
public void onClick(@NonNull View v) {
if (mResolvedMethod == null) {
resolveMethod(mHostView.getContext(), mMethodName);
}
...
}
然后通过方法名去查找对应的方法,下面就是查找的具体实现
private void resolveMethod(@Nullable Context context, @NonNull String name) {
...
final Method method = context.getClass().getMethod(mMethodName, View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
...
}
final Method method = context.getClass().getMethod(mMethodName, View.class);
这里的通过利用上下文,也就是设置布局的类,去获取到该类中对应方法名mMethodName及参数为View的方法。
这样就完成了onClick的属性值布置了。
Android中,View在layout文件的onClick属性值,是如何找到对应的onClick方法过程(个人理解)
最新推荐文章于 2021-05-27 10:32:54 发布