简体   繁体   中英

Android R.java use

When we add some entries in the strings.xml file or layout.xml file, then the R.java file gets modified automatically. Again, if we want to refer something from the layout file such as reading the EditText value entered by the user, then again we refer the R.java file at our java code to read the values.

What is this R.java file all about? The values of each entry of this R.java file seems to be in HEXADECIMAL format but what is its use?

I have read the doc but i get fairly confused for this R.java :(

Please someone step forward and explain what is this R.java file all about :(

Regards,

http://developer.android.com/guide/topics/ui/declaring-layout.html says:

 android:id="@+id/my_button" 

The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file).

The R.java file is generated by the Android Resource Manager ( aapt.exe ) and contains references to all resources of your app. Each reference is a unique id (public static final int). These constants are written to the R.java file in hexadecimal format. The logic of assigning specific integer to each resource is private to the Android Resource Manager. You can look at the source code of aapt.exe on the internet, eg at http://gitorious.org/rowboat/frameworks-base/trees/d58fb97ddf052b3ceac921ac7e936af990392b2c/tools/aapt

它将资源对象转换为Java可识别的名称,以供您在代码中引用。

The R class is basically the way Android provide resource access from within your code. As you wrote when you alters strings.xml etc on save of that resource file Android SDK will recompile the R class to make your changes accessible for within your code. What the values in R class are is more or less not important since its used by Android internally to map, for example a string to an ID.

To reference a string from within your code you use R like this:

R.string.MyString

If you string in string.xml is called MyString. Same for layouts etc.

I guess this is what you read, but otherwise it's pretty good explained here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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