简体   繁体   English

Android studio:如何自动生成从 xml 到 java 类的视图变量?

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

I have the following activity xml file:我有以下活动 xml 文件:

在此处输入图片说明

I would like to generate all the views variables in the java file base on their name.我想根据名称生成 java 文件中的所有视图变量。

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.为任何新活动重新做一遍是非常令人沮丧的,而且我似乎基本不在 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在 java for 中没有开发findviewByid()你可以使用像Butter Knife这样的findviewByid()

or use android native DataBinding或使用 android 原生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.在最近发布的 Android API 中, ViewBinding是操作视图组件的推荐方式。

However, if you really need the classic findViewById(), here are several plugins that you can use in Android Studio:然而,如果你真的需要经典的 findViewById(),这里有几个你可以在 Android Studio 中使用的插件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM