#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开发中,可以使用`onClick`属性来指定一个View的点击事件处理方法。具体使用方法如下: 1. 在布局文件中,给需要绑定点击事件的View添加`android:onClick`属性,值为一个方法名,例如: ```xml <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:onClick="onButtonClick"/> ``` 2. 在对应的Activity或Fragment中,定义一个与`onClick`属性绑定的方法,例如: ```java public void onButtonClick(View view) { // 处理点击事件 } ``` 需要注意的是,方法名必须与`onClick`属性的值相同,并且方法的参数必须是`View`类型,否则会导致运行时异常。 示例代码如下: ```xml <!-- activity_main.xml --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/myButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:onClick="onButtonClick"/> </RelativeLayout> ``` ```java // MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onButtonClick(View view) { Toast.makeText(this, "Button clicked", Toast.LENGTH_SHORT).show(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值