为什么XML onClick属性在AppCompatButtons上设置OnClickListener TWICE

时间:2019-05-07 22:11:14

标签: android android-layout android-appcompat layout-inflater appcompatactivity

当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通胀过程中。 那是个错误吗?还是该行为的目的是什么?

0 个答案:

没有答案