Why does a protected android:onClick method in Activity actually work?

前端 未结 2 1849
广开言路
广开言路 2021-02-01 15:51

Suppose you define android:onClick=\"doClick\" in your Activity as

protected void doClick(View view) { }

The document

相关标签:
2条回答
  • 2021-02-01 16:07

    As per "The Java™ Tutorials" : The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package

    0 讨论(0)
  • 2021-02-01 16:15

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

    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.

    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.

    0 讨论(0)
提交回复
热议问题