java Android:如何从布局文件自动生成Java代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7462022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:04:40  来源:igfitidea点击:

Android: How to automatically generate Java code from layout file?

javaandroidlayout

提问by hsoetikno

Does anyone know of any Eclipse plug-in or anything that can be used to automatically generate Java code from layout file? As in, if I have an EditText in my layout file with the ID "@+id/txtHello", I expect something like the following to be generated:

有谁知道任何 Eclipse 插件或任何可用于从布局文件自动生成 Java 代码的插件?例如,如果我的布局文件中有一个 ID 为“@+id/txtHello”的 EditText,我希望生成如下内容:

EditText txtHello = (EditText) findViewById(R.id.txtHello);

Thanks for your time!

谢谢你的时间!

Harris :)

哈里斯 :)

采纳答案by Diego Torres Milano

There is an Eclipse plugin that does exactly what you want , I guess: http://marketplace.eclipse.org/content/lazy-android

有一个 Eclipse 插件可以完全满足您的需求,我想:http: //marketplace.eclipse.org/content/lazy-android

回答by ChrLipp

Normally there are three different ways to do this:

通常有三种不同的方法来做到这一点:

  1. at run time (via annotations per reflection)
  2. at compile time (via annotations or aspects)
  3. at development time (via code generators)
  1. 在运行时(通过每个反射的注释)
  2. 在编译时(通过注解或方面)
  3. 在开发时(通过代码生成器)

A good article to start is Clean Code in Android Applications.

一篇很好的文章开始是Android 应用程序中的清洁代码

Ad 1) Two solutions, see

广告 1) 两种解决方案,请参阅

Ad 2) Android Annotations, see http://androidannotations.org/

广告 2) Android 注释,请参阅http://androidannotations.org/

Ad 3) Two solutions, see

广告 3) 两种解决方案,请参阅

If there is more, please tell! I personally prefer 2) and therefore Android Annotations.

如果有更多,请告诉!我个人更喜欢 2),因此更喜欢 Android 注释。

Hope that helps!

希望有帮助!

回答by Andreas Rudolph

Use this online tool:

使用此在线工具:

http://www.buzzingandroid.com/tools/android-layout-finder/

http://www.buzzingandroid.com/tools/android-layout-finder/

It simply gets the job done quickly. I use it every day.

它只是快速完成工作。我每天都使用它。

回答by tmorcinek

There is new plugin that can generate Activity, Fragment, Adapter based on xml layout. Can also generate Menu related code (handling actions) for xml menu files. And has editable templates, so the user has more control on the generated data.

有新的插件可以基于xml布局生成Activity、Fragment、Adapter。也可以为xml菜单文件生成Menu相关的代码(处理动作)。并且具有可编辑的模板,因此用户可以更好地控制生成的数据。

For Eclipse: http://tmorcinek.github.io/android-codegenerator-plugin-eclipse/

对于 Eclipse:http: //tmorcinek.github.io/android-codegenerator-plugin-eclipse/

For Android Studio/IntelliJ IDEA: http://tmorcinek.github.io/android-codegenerator-plugin-intellij/

对于 Android Studio/IntelliJ IDEA:http: //tmorcinek.github.io/android-codegenerator-plugin-intellij/

回答by iamkristher

I use MotoDev Studio. It really saves time.

我使用 MotoDev Studio。它确实节省了时间。