当Android使用XML属性Button
夸大onClick
时,它会在内部DeclaredOnClickListener
上设置Button
,然后使用反射来触发实际的{{1} }方法中的代码。
onClick
我注意到,对于case R.styleable.View_onClick:
[...]
final String handlerName = a.getString(attr);
if (handlerName != null) {
setOnClickListener(new DeclaredOnClickListener(this, handlerName));
}
break;
个(即AppCompatButton
中的普通Buttons
),在AppCompatActivity
类中重复了相同的过程,导致了两个不同的{{1 }}连续设置。
AppCompatViewInflater
两个方法都被执行,并且两个DeclaredOnClickListener
都被创建并设置在private void checkOnClickListener(View view, AttributeSet attrs) {
Context context = view.getContext();
if (context instanceof ContextWrapper && (VERSION.SDK_INT < 15 || ViewCompat.hasOnClickListeners(view))) {
TypedArray a = context.obtainStyledAttributes(attrs, sOnClickAttrs);
String handlerName = a.getString(0);
if (handlerName != null) {
view.setOnClickListener(new AppCompatViewInflater.DeclaredOnClickListener(view, handlerName));
}
a.recycle();
}
}
上。首先使用DeclaredOnClickListeners
,然后替换为Button
。这一切都发生在View
通胀过程中。
那是个错误吗?还是该行为的目的是什么?