#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. Android Studio打包生成Jar包的方法(亲测可用)
  2. android 常用控件一览(从底层分析,为自定义控件做下小铺垫)
  3. Android更新UI的方法
  4. Android监听底层事件的机制总结
  5. Eclipse中编译Android工程时出现的问题解决方法。
  6. Android使用反射机制设置ListView的默认焦点

随机推荐

  1. Android 短信 彩信 wap push的接收
  2. 最新Android ADT, SDK, SDK_tool等官方下
  3. android sms发送、接收及格式
  4. Android布局
  5. [置顶] Android(安卓)性能优化(一)内存篇
  6. Android 布局属性大全
  7. Android SDK更新以及ADT更新出现问题的解
  8. SeekBar自定义
  9. 把TextView中的文字添加阴影效果及Style
  10. android开发,修改默认界面的背景色