#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;            ......        }    }}

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. Android实现退出时关闭所有Activity的方法
  5. Android(安卓)- 多线程 - AsyncTask
  6. Android开发艺术探索——第二章:IPC机制(上)
  7. Android异步任务AsyncTask
  8. android Q 显示系统(一) VSync
  9. Android自带音频均衡器MusicFx分析

随机推荐

  1. android Eclipse导入com.android.interna
  2. Android、java环境搭建流程
  3. Android(安卓)SQLiteOpenHelper的使用
  4. Android各个版本的区别
  5. Android可以在子线程更新UI吗
  6. 【android】HandlerThread的使用及源码剖
  7. Android原始视频格式YUV,NV12,NV21,YV12,YU
  8. Android(安卓)Studio 布局中引用自定义属
  9. Android(安卓)Dialog,Toast封装
  10. Android(安卓)gradle打包并自动上传