简体   繁体   English

xml文件和java文件如何在android中相互交互?

[英]How do xml file and java file interact each other in android?

I'm learning android about a month. 我正在学习android大约一个月的时间。 As I study onClickListner and xml's onClick attribute, I got a question. 在研究onClickListner和xml的onClick属性时,我遇到了一个问题。
After I write the code android:onClick="showMap" on xml and "public void showMap(View view)" on java file, the application make a button which can be perform the showMap method. 在xml上编写代码android:onClick="showMap"和在Java文件上编写代码“ public void showMap(View view)”之后,应用程序制作了一个可以执行showMap方法的按钮。
I could understand the onClickListner control the xml element by id( R.id.somthing ). 我能理解onClickListner通过ID(控制XML元素R.id.somthing )。

How can the showMap method notice the method act as the attribute of the very xml element? showMap方法如何注意该方法充当xml元素的属性? I found some other question and it said it can be because of the view parameter, but I'm not convinced perfectly. 我发现了另一个问题,它说这可能是由于view参数引起的,但我并没有完全确信。 How do xml and java file interact each other? xml和java文件如何相互交互? Please let me know their internal interaction or which the source code do I have to see to understand my question. 请让我知道它们的内部交互作用,或者我必须查看哪些源代码才能理解我的问题。

Thank you. 谢谢。

ps When I asked to my friend who is good at android, he answered me to find "xml parsing algorithm". ps当我问一位擅长android的朋友时,他回答我找到“ xml解析算法”。 But I couldn't find satisfying result. 但是我找不到令人满意的结果。

The xml is parsed a compile time and the corresponding View's object is created, with the attributes you specified. xml将在编译时进行解析,并使用您指定的属性创建相应的View对象。 The method you declare in the View's onClick property is managed through reflection. 您在View的onClick属性中声明的方法是通过反射进行管理的。 You can take a look to it here 你可以在这里看看

与我们的应用程序捆绑在一起的xml文件(出于性能原因,通过aapt / 2转换为二进制xml),然后在运行时使用LayoutInflater对其进行膨胀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM