简体   繁体   中英

Android studio: How to auto-generate views variables from xml to java class?

I have the following activity xml file:

在此处输入图片说明

I would like to generate all the views variables in the java file base on their name.

For this example:

public class RegisterActivity extends AppCompatActivity {
    private EditText editText_firstname;
    private TextView label_lastname;
    // .. So on

    private void setup_views() {
         editText_firstname = (EditText) findViewById(R.id.editText_firstname);
         label_lastname = (TextView) findViewById(R.id.label_lastname);
         // .. So on
    }
}

It very frustrating to do it all over again for any new activity, and I seems to basic to not be in android studio.

So, how do I do it?

Thanks in advance.

in java for did not develop findviewByid() you can use dataBinding library like Butter Knife

or use android native DataBinding

如上所述的数据绑定或切换到 Kotlin,您将不需要findViewById

在 Java 中它可能令人沮丧,但如果你切换到 Kotlin,使用这个伟大的 Anko会更容易绘制一个 lyaout

Android Studio 有一个插件可以让你这样做,请参考以下链接获取插件: https : //plugins.jetbrains.com/plugin/7595-android-code-generator

In the recent release of Android API, ViewBinding is the recommended way to manipulate view components.

However, if you really need the classic findViewById(), here are several plugins that you can use in Android Studio:

  1. https://plugins.jetbrains.com/plugin/11204-android-findviewbyid-support
  2. https://plugins.jetbrains.com/plugin/7595-android-code-generator
  3. https://github.com/laobie/FindViewByMe
  4. https://github.com/dotnet-ad/AutoFindViews

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