#Android源代码#android:onClick属性的底层调用
16lz
2021-12-04
#Android源代码#android:onClick属性的底层调用
原理
在View类的构造方法中发现这样的一个有趣的东西,可见。我们在布局中使用android:onClick="functionName"
,底层其实是设置好了监听器了,然后利用反射调用我们在代码中写的方法,如:
//在Activity中写的代码public void functionName(View v){ //doSomthing}
//View中反射调用Method mHandler = getContext().getClass().getMethod(handlerName,View.class);mHandler.invoke(getContext(), View.this);
可见,当方法不为public时调用失败,抛出异常,因为这里并没有进行暴力反射;
当方法不包含类型为View的参数时也会调用失败,抛出异常;
当返回值不为void时,调用仍然成功!
View.java的构造方法
public View(Context context, AttributeSet attrs, int defStyle) { this(context); TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View, defStyle, 0); ...... 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 OnClickListener() { private Method mHandler; public void onClick(View v) { if (mHandler == null) { try { mHandler = getContext().getClass().getMethod(handlerName, View.class); } catch (NoSuchMethodException e) { int id = getId(); String idText = id == NO_ID ? "" : " with id '" + getContext().getResources().getResourceEntryName( id) + "'"; throw new IllegalStateException("Could not find a method " + handlerName + "(View) in the activity " + getContext().getClass() + " for onClick handler" + " on view " + View.this.getClass() + idText, e); } } try { mHandler.invoke(getContext(), View.this); } catch (IllegalAccessException e) { throw new IllegalStateException("Could not execute non " + "public method of the activity", e); } catch (InvocationTargetException e) { throw new IllegalStateException("Could not execute " + "method of the activity", e); } } }); } break; ...... } }}
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- Android实现退出时关闭所有Activity的方法
- Android(安卓)- 多线程 - AsyncTask
- Android开发艺术探索——第二章:IPC机制(上)
- Android异步任务AsyncTask
- android Q 显示系统(一) VSync
- Android自带音频均衡器MusicFx分析