安卓:自动进行变量在XML中,所有的ID [英] Android: automatically make variables for all IDs in xml

查看:320
本文介绍了安卓:自动进行变量在XML中,所有的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到了Android开发的最繁琐的部分之一是布局设计,甚至与布局建设者。

I noticed one of the most tedious parts of Android development is the layout design, even with layout builder.

设置图形,然后布局,使得可变协会与布局元素之后是非常繁琐的,如的ImageButton myButton的=(的ImageButton)findViewById(R.id.myButton);

After setting up the graphics, then the layout, making variable associations with the layout elements is very tedious, such as ImageButton myButton = (ImageButton)findViewById(R.id.myButton);

在更大的布局,这些都可以让繁琐的跟踪(忆元素的名称),然后需要在任何类型的增加更多的变数才能得到令人沮丧的。

in larger layouts, these can get tedious to keep track of (recalling the names of the elements) and then the need to add more variables in any kind of order gets frustrating.

要稍微缓和这一点,那将是非常方便的,如果所有我在XML中声明的ID被自动与其正确的变量相关,并在该类所有这些数据类型已经列入

To slightly mitigate this, it would be very convenient if all of the IDs I declared in the XML were automatically associated with their proper variables, and all of those datatypes were already included in that class

有一些已经这样做了?

例如,如果我写

 <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/myButton" android:id="@+id/myButton"></ImageButton>

那么,我想,其中包括这个布局已经有了类

then I would like the classes which include this layout to already have

 import android.ImageButton;

 ImageButton myButton;

 myButton = (ImageButton)findViewById(R.id.myButton);

这是一个设置或功能要求?我使用的Eclipse IDE中,这将是非常方便的。

is this a setting or a feature to request? I am using the Eclipse IDE and it would be very convenient

推荐答案

请尝试使用 Android的注解。它提供了有用的注解来代替样板code。

Try using Android Annotations. It provides useful annotations to replace boilerplate code.

有关实例,请参阅 @ViewById 文档:刚刚宣布注明领域

For instance, see @ViewById documentation: just declare the fields annotated

@ViewById
EditText myEditText;

@ViewById(R.id.myTextView)
TextView textView;

它取代

EditText myEditText;

TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
    [...]
    myEditText = (EditText) findViewById(R.id.myEditText);
    textView = (TextView) findViewById(R.id.myTextView);
}

这篇关于安卓:自动进行变量在XML中,所有的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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