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

本文详细解析了Android源代码中onClick属性的底层实现机制,包括如何通过反射调用Activity中的自定义方法,以及方法的可见性、参数类型和返回值对调用的影响。同时,阐述了在不同情况下onClick方法的调用过程和可能出现的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
            ......
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值