如何在Android XML属性安卓的onClick =" ..."幕后工作?

人气:1,034 发布:2022-09-11 标签: xml java android

问题描述

我问这一点,因为即时通讯intrested,大多是因为我怕使用性能\ code,我不明白。从我的理解在java中工作的所有事件,通过实施监听inteface那适当的事件。的onClick =...属性:如果我在我的活动实现的onClick 2按钮和第三个定义一个方法与Android将会发生什么情况?我发现这个文章,所以看完后我明白,它使用的是匿名类probeb​​ly实施实现OnClickListener,  但我想,以确保\多了解一下,用什么方式我会碰坏\利用这些知识对我advantege?在Android 参考是不清楚它是如何工作,,,

I'm asking this a little because im intrested and mostly because im afraid to use properties\code that I don't understand. From what I understand all events in java work by implementing a listener inteface thats appropriate to the event. What will happen if I implement onClick in my activity for 2 buttons and for the third define a method with the android:onClick="..." property? I found this article, so after reading it I understand that its probebly implemented using a anonymous class that implements the OnClickListener, but I'd like to be sure\know more about it, and in what way I might break something\ use this knowledge to my advantege? the android reference isn't clear about how it works,,,

推荐答案

它使用反射来找出在运行时要调用的方法。这是一个视图的属性,因此该视图有相关的code,看起来,如果此属性设置,然后找出方法名称上的活动,并触发它。

It uses reflection to figure out the method to call at runtime. It's a view's property, so the View has the relevant code which looks if this property is set, and then figure out the method name on the activity and triggers it.

查看总是绑定到特定活动的范围内,所以,能够通过反射调用该公共方法。

View is always bound to a specific activity's context, and so, is able to call this public method through reflection.

您可以看到视图类的源$ C ​​$ C在这里。你可以看到行号2003,在这种情况下被处理。

You can see the source code of the view class here. You can see line number 2003, where this situation is being handled.

Source $ C $下View类

660