Understand class R in Android

In android, I'm not sure I fully understand class R I am looking through a sudoku example and I have this piece of code:

 switch (v.getId()) // the id of the argument passed is evaluated by switch statement { case R.id.about_button: // Intent i = new Intent(this, about.class); startActivity(i); break; // More buttons go here (if any) ... } 

I am new to Java, but from what I collect, it looks like it takes input (the touch screen touches the button) and evaluates the argument. Then the case statement is set if the about button is known, and a new interface screen is created, and then transferred to the phone.

Is it correct?

If I understood the essence of this question, why the deal with the class "R"?

Why is it called to recognize the button identifier?

I thought the superclass (in this project) was the SudokuActivity class.

+63
java android sudoku
Jul 23 '11 at 23:55
source share
3 answers

R.java is a dynamically generated class created during the build process to dynamically identify all assets (from rows to android widgets to layouts) for use in Java classes in an Android application. Please note that this R.java has Android specifics (although you can duplicate it for other platforms, which is very convenient), so it does not have much to do with the Java language constructs. Look here for more details.

+91
Jul 24 2018-11-11T00:
source share

R is a class containing ONLY public constants. (public static finale).

This is a generated class (from Android Plugin in Eclipse) that reflects the various values ​​you define in the res file.

For example, you should have something like:

 android:id="@+id/about_button" 

somewhere in one of your layout / xml menu files in the project, and once you have written this, Eclipse will generate a constant in the R file (which you can find in gen/PACKAGE/R.java )

Learn more about this resource guide in Android Developers .

+39
Jul 24 2018-11-11T00:
source share

R class is generated by Android tools from your resources before compiling your code. It contains an assigned numeric constant for each resource that you can reference in your project. For example, you have an XML resource file containing about_button . If you did not have class R , you would need to use the string "about_button" to refer to it in the code. If you make a mistake in this line, you will only know about it when you start the application. With R you will see an error much earlier during compilation.

R structured in such a way that you can reference resources through your inner classes. For example, R.id contains id constants and R.layout contains layout constants.

+19
Jul 24. '11 at 0:13
source share



All Articles