为什么Activity中受保护的android:onClick方法实际上有效? [英] Why does a protected android:onClick method in Activity actually work?

查看:352
本文介绍了为什么Activity中受保护的android:onClick方法实际上有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您在活动中定义 android:onClick =doClick

protected void doClick(View view) { }

文档指出


此名称必须对应一个公共方法,该方法只接受View类型的一个参数。

This name must correspond to a public method that takes exactly one parameter of type View.

这是基础 Class.getMethod()方法的给定要求,该方法仅查找公共方法文档说明它

This is a given requirement of the underlying Class.getMethod() method, which only finds public methods as the documentation states that it


返回方法对象,该对象反映此 object。

Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.

那怎么可能,这个不应该工作的实现适用于某些设备和模拟器,而对其他使用相同API级别的实现不起作用?

So how is it possible, that this implementation, which should not work at all, works on some devices and emulators, while it doesn't work on others using the same API levels as well?

推荐答案

我调试了特定的实现。代码的相关部分位于支持库中,使用 Class.getMethod()

I debugged the particular implementations. The relevant portion of the code is within the Support Library using Class.getMethod().

如文档中所述,此方法仅查找公共成员方法并且行为正确。出于某种原因, Activity 的所有声明受保护方法的修饰符(这些是 onCreate() doClick())设置为 1 ,这意味着它们实际上是公开的。

As stated in the documentation, this method only finds public member methods and behaves correctly. For some reason, all modifiers of declared protected methods of the Activity (these are onCreate() and doClick()) are set to 1, which means these are actually public.

我只能观察到使用Mac创建 debug 版本的这种行为。那么为什么会发生这种情况仍然是一个悬而未决的问题,我正试图找到答案。

I could only observe this behavior creating the debug build with a Mac. So why this happens is still an open question, which I'm trying to find an answer to.

这篇关于为什么Activity中受保护的android:onClick方法实际上有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆