了解Android的R类。

时间:2023-02-09 15:42:48

In android, I'm not sure I quite understand the R class. I'm going through the sudoku example, and I have this snippet of code:

在安卓系统中,我不确定我是否完全理解R类。我要讲数独的例子,我有这段代码:

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'm brand new to Java, but from what I gather it looks like it's taking input (the touch screen being touched on the button) and evaluating the argument. Then the case statement is setup if the about button is recognized, and a new interface screen is created and then navigated to on the phone.

我是Java的新手,但是从我收集的东西来看,它看起来像是在接收输入(触摸屏幕被触摸到),并对参数进行评估。然后,如果确认了about按钮,就设置了case语句,然后创建一个新的界面屏幕,然后通过手机导航。

Is this right?

这是正确的吗?

If I got the gist of that correct, why is the deal with the "R" class?

如果我知道正确的要点,为什么要处理“R”类?

Why is it called to recognize the ID of the button?

为什么叫它识别按钮的ID ?

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

我认为超类(在这个项目中)是SudokuActivity类。

3 个解决方案

#1


73  

R.java is the dynamically generated class, created during build process to dynamically identify all assets (from strings to android widgets to layouts), for usage in java classes in Android app. Note this R.java is Android specific (though you may be able to duplicate it for other platforms, its very convenient), so it doesn't have much to do with Java language constructs. Take a look here, for more details.

R。java是动态生成的类,在构建过程中创建,以动态地识别所有资产(从字符串到android小部件到布局),用于android应用程序中的java类。java是特定于Android的(尽管您可能可以将它复制到其他平台上,它非常方便),因此它与java语言构造没有太大的关系。请看这里,了解更多细节。

#2


32  

R is a class that contains ONLY public constants. (public static final).

R是一个只包含公共常量的类。(公共静态决赛)。

It is a generated class (by Android Plugin in Eclipse) that reflects the various values you defined in the res file.

它是一个生成的类(通过Eclipse中的Android插件),它反映了在res文件中定义的各种值。

For instance, you should have something like:

例如,你应该有:

android:id="@+id/about_button"

somewhere in one of your layout/menu xml file in the project, and once you wrote that, Eclipse will generate a constant in the R file (which you can find it under gen/PACKAGE/R.java)

在项目中的某个布局/菜单xml文件中,Eclipse将在R文件中生成一个常量(您可以在gen/PACKAGE/R.java下找到它)。

Read the Resource guide in Android Developers for more information about this.

请阅读Android开发人员的资源指南,了解更多相关信息。

#3


16  

R class is generated by Android tools from your resources before compiling your code. It contains assigned numeric constant for each resource that you can refer in your project. For example, you have XML resource file that contains about_button. If you didn't have R class, you would have to use a string "about_button" to refer to it in code. If you make a mistake in this string, you will only learn about it when you run your application. With R you will see the error much earlier at compile time.

在编译代码之前,R类是由来自您的资源的Android工具生成的。它包含分配给项目中可以引用的每个资源的数值常量。例如,您有一个包含about_button的XML资源文件。如果没有R类,则必须使用string“about_button”来在代码中引用它。如果您在这个字符串中出错,您将只在运行应用程序时了解它。使用R,您将在编译时更早地看到错误。

R is structured in such a way that you can refer to resources via its inner classes. For example, R.id contains id constants and R.layout contains layout constants.

R的结构是这样的,您可以通过它的内部类引用资源。例如,R。id包含id常量和R。布局包含布局常数。

#1


73  

R.java is the dynamically generated class, created during build process to dynamically identify all assets (from strings to android widgets to layouts), for usage in java classes in Android app. Note this R.java is Android specific (though you may be able to duplicate it for other platforms, its very convenient), so it doesn't have much to do with Java language constructs. Take a look here, for more details.

R。java是动态生成的类,在构建过程中创建,以动态地识别所有资产(从字符串到android小部件到布局),用于android应用程序中的java类。java是特定于Android的(尽管您可能可以将它复制到其他平台上,它非常方便),因此它与java语言构造没有太大的关系。请看这里,了解更多细节。

#2


32  

R is a class that contains ONLY public constants. (public static final).

R是一个只包含公共常量的类。(公共静态决赛)。

It is a generated class (by Android Plugin in Eclipse) that reflects the various values you defined in the res file.

它是一个生成的类(通过Eclipse中的Android插件),它反映了在res文件中定义的各种值。

For instance, you should have something like:

例如,你应该有:

android:id="@+id/about_button"

somewhere in one of your layout/menu xml file in the project, and once you wrote that, Eclipse will generate a constant in the R file (which you can find it under gen/PACKAGE/R.java)

在项目中的某个布局/菜单xml文件中,Eclipse将在R文件中生成一个常量(您可以在gen/PACKAGE/R.java下找到它)。

Read the Resource guide in Android Developers for more information about this.

请阅读Android开发人员的资源指南,了解更多相关信息。

#3


16  

R class is generated by Android tools from your resources before compiling your code. It contains assigned numeric constant for each resource that you can refer in your project. For example, you have XML resource file that contains about_button. If you didn't have R class, you would have to use a string "about_button" to refer to it in code. If you make a mistake in this string, you will only learn about it when you run your application. With R you will see the error much earlier at compile time.

在编译代码之前,R类是由来自您的资源的Android工具生成的。它包含分配给项目中可以引用的每个资源的数值常量。例如,您有一个包含about_button的XML资源文件。如果没有R类,则必须使用string“about_button”来在代码中引用它。如果您在这个字符串中出错,您将只在运行应用程序时了解它。使用R,您将在编译时更早地看到错误。

R is structured in such a way that you can refer to resources via its inner classes. For example, R.id contains id constants and R.layout contains layout constants.

R的结构是这样的,您可以通过它的内部类引用资源。例如,R。id包含id常量和R。布局包含布局常数。