android android:onclick小记

本文详细介绍了在XML中添加android:onclick关键字时需注意的事项,包括函数参数、修饰符以及反射机制的使用。
部署运行你感兴趣的模型镜像

在XML中添加android:onclick关键字需注意事项:

1.被生命的函数的参数必须是“(View view)”;

2.函数必须被修饰为public而不能使private或protected。

 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);
                                }
                            }
                        });
                    }

源码中使用反射机制中的getMthod方法,getMethod()只返回Public的方法 ,getDeclaredMethods();可以返回所有的函数,包括private 的函数  ,因此必须修饰为public.

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.9

TensorFlow-v2.9

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/tvResult" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="32sp" android:text="0" android:gravity="end" android:padding="16dp"/> <GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="4" android:rowCount="5"> <!-- 第一行 --> <Button android:text="C" android:onClick="onClearClick" android:layout_columnSpan="2" android:layout_width="0dp" android:layout_columnWeight="1"/> <Button android:text="Del" android:onClick="onDeleteClick" android:layout_columnSpan="2" android:layout_width="0dp" android:layout_columnWeight="1"/> <!-- 数字和运算符 --> <Button android:text="7" android:onClick="onNumberClick"/> <Button android:text="8" android:onClick="onNumberClick"/> <Button android:text="9" android:onClick="onNumberClick"/> <Button android:text="÷" android:onClick="onOperationClick"/> <Button android:text="4" android:onClick="onNumberClick"/> <Button android:text="5" android:onClick="onNumberClick"/> <Button android:text="6" android:onClick="onNumberClick"/> <Button android:text="×" android:onClick="onOperationClick"/> <Button android:text="1" android:onClick="onNumberClick"/> <Button android:text="2" android:onClick="onNumberClick"/> <Button android:text="3" android:onClick="onNumberClick"/> <Button android:text="-" android:onClick="onOperationClick"/> <Button android:text="0" android:onClick="onNumberClick"/> <Button android:text="." android:onClick="onDecimalClick"/> <Button android:text="=" android:onClick="onEqualClick"/> <Button android:text="+" android:onClick="onOperationClick"/> </GridLayout> </LinearLayout>详细解释代码
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值