#Android源代码#android:onClick属性的底层调用

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

你可能感兴趣的:(android,源代码,view,onclick)