可以/我应该编辑R.java文件。如果是这样,怎么样? [英] Can/should I edit the R.java file. If so, how?

查看:138
本文介绍了可以/我应该编辑R.java文件。如果是这样,怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在Eclipse for Android 4.0中编写了一个简单的计算器,我正在尝试对我的代码进行流式处理,并使其尽可能简单。我正在尝试清理的地方是我的findViewById()。由于我有按钮0-9来实例化我有一个十行长的代码块,如下所示:

So I'm programming a simple calculator in Eclipse for Android 4.0 and I'm trying to stream-line my code and make it as simple as possible. The place I'm trying to clean up is my findViewById()'s. Since I have buttons 0-9 to instantiate I have a block of code ten lines long that look like this:

b0 = (Button) findViewById(R.id.b0);
b1 = (Button) findViewById(R.id.b1);
...
b9 = (Button) findViewById(R.id.b9);

正如你所看到的那样,这就是求助于for循环。所以我想做的是制作两个数组。 Activity中的一个实例变量数组,它包含数字键盘的所有Button实例变量:

As you can see this thing is just begging for a for-loop. So what I wanted to do was make two arrays. One instance variable array in the Activity which holds all the Button instance variables for the number pad:

private Button[] numberPad = {b0,b1,b2,b3,b4,b5,b6,b7,b8,b9};

然后是另一个包含所有按钮id变量的R.java文件中id类的数组看起来像这样:

and then another array in the id class in the R.java file that holds all the button id variables that would look like this:

private static final int[] numberPad = {b0,b1,b2,b3,b4,b5,b6,b7,b8,b9};

所以我可以使用这个循环减少十行Button实例化为两个:

Sooo that I can reduce ten lines of Button instantiations to two using this loop:

for(int i = 0; i < numberPad.length; i++)
    numberPad[i] = (Button) findViewById(R.id.numberPad[i]);

当我输入它时没关系,但是当我去保存它时它会自动恢复到它的状态原始形式。我没有看到这个代码有什么问题。据我所知,它不会产生任何错误。为什么我不能以这种方式编辑R.java文件?有办法吗?我应该吗?

When I type it out it's fine, but when I go to save it it reverts back automatically to it's original form. I don't see anything wrong with this code. It doesn't produce any errors as far as I can tell. Why can't I edit the R.java file in this way? Is there a way to? Should I?

推荐答案

如前所述,R文件是(或应该)在每次构建时自动重新生成。你应该尝试一些依赖注入框架( Roboguice 适用于Android编程)来清理问题处理视图。例如,代替(从项目文档中获取的代码):

As said before, R file is (or should be) re-generated automatically on every build. You should try some dependency-injection framework (Roboguice works well for Android programming) to clean up issues with handling Views. For example instead of (code taken from project's documentation):

class AndroidWay extends Activity { 
TextView name; 
ImageView thumbnail; 
LocationManager loc; 
Drawable icon; 
String myName; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);
    name      = (TextView) findViewById(R.id.name); 
    thumbnail = (ImageView) findViewById(R.id.thumbnail); 
    loc       = (LocationManager) getSystemService(Activity.LOCATION_SERVICE); 
    icon      = getResources().getDrawable(R.drawable.icon); 
    myName    = getString(R.string.app_name); 
    name.setText( "Hello, " + myName ); 
}} 

您可以使用更简单更短的版本:

You can use simpler and shorter version:

class RoboWay extends RoboActivity { 
@InjectView(R.id.name)             TextView name; 
@InjectView(R.id.thumbnail)        ImageView thumbnail; 
@InjectResource(R.drawable.icon)   Drawable icon; 
@InjectResource(R.string.app_name) String myName; 
@Inject                            LocationManager loc; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);
    name.setText( "Hello, " + myName ); 
} } 

这不仅有助于保持视图管理的清洁和就地,在测试方面也非常有用。

This not only helps keeping your Views management clean and in-place but is also very helpful when it comes to testing.

这篇关于可以/我应该编辑R.java文件。如果是这样,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆