将 Activity 添加到 Eclipse 中的 Android 项目的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2337874/
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-08-20 05:28:01  来源:igfitidea点击:

Best way to add Activity to an Android project in Eclipse?

androideclipseideandroid-activity

提问by Eno

When adding an activity to an existing Android project, I manually create a new class - is that the best / preferred way? How do others handle that?

向现有 Android 项目添加活动时,我手动创建了一个新类 - 这是最佳/首选方式吗?其他人如何处理?

回答by Wilka

You can use the "New Class" dialog, but that leaves other steps you need to do by hand (e.g. adding an entry to the manifest file). If you want those steps to be automated, you can create the activity via the manifest editor like this:

您可以使用“新建类”对话框,但这留下了您需要手动执行的其他步骤(例如,向清单文件添加条目)。如果您希望这些步骤自动化,您可以通过清单编辑器创建活动,如下所示:

  1. Double click on AndroidManifest.xml in the package explorer.
  2. Click on the "Application" tab of the manifest editor
  3. Click on "Add.." under the "Application Nodes" heading (bottom left of the screen)
  4. Choose Activity from the list in the dialog that pops up (if you have the option, you want to create a new top-level element)
  5. Click on the "Name*" link under the "Attributes for" header (bottom right of the window) to create a class for the new activity.
  1. 双击包浏览器中的 AndroidManifest.xml。
  2. 单击清单编辑器的“应用程序”选项卡
  3. 单击“应用程序节点”标题下的“添加..”(屏幕左下角)
  4. 从弹出的对话框中的列表中选择活动(如果有选项,您要创建一个新的顶级元素)
  5. 单击“Attributes for”标题下的“Name*”链接(窗口右下角),为新活动创建一个类。

When you click Finish from the new class dialog, it'll take you to your new activity class so you can start coding.

当您从新类对话框中单击完成时,它会将您带到新的活动类,以便您可以开始编码。

Five steps might seem a lot, but I'm just trying to be extra detailed here so that it's clear. It's pretty quick when you actually do it.

五个步骤可能看起来很多,但我只是想在这里更加详细,以便清楚。当你真正做到这一点时,它会很快。

回答by inankupeli

It is now much easier to do this in Eclipse now. Just right click on the packagethat will contain your new activity. New -> Other -> (Under Android tab) Android Activity.

现在在 Eclipse 中执行此操作要容易得多。只需右键单击将包含您的新活动的包新建 -> 其他 ->(在 Android 选项卡下)Android 活动。

And that's all. Your new activity is automatically added to the manifest file as well.

就这样。您的新活动也会自动添加到清单文件中。

回答by Shankar Damodaran

An easy method suggested by Google Android Developer Community.

Google Android 开发者社区建议的一种简单方法。

enter image description here

在此处输入图片说明

回答by Mark B

I just use the "New Class" dialog in Eclipse and set the base class as Activity. I'm not aware of any other way to do this. What other method would you expect to be available?

我只是在 Eclipse 中使用“新建类”对话框并将基类设置为活动。我不知道有任何其他方法可以做到这一点。您希望还有哪些其他方法可用?

回答by Jerry Brady

The R.* classes are generated dynamically. I leave the "Build automatically" option on in the Project menu so that mine R.* classes are always up-to-date.

R.* 类是动态生成的。我在“项目”菜单中保留“自动构建”选项,以便我的 R.* 类始终是最新的。

Additionally, when creating new Activities, I copy and rename old ones, especially if they are similar to the new Activity that I need because Eclipse renames everything for you.

此外,在创建新活动时,我会复制并重命名旧活动,尤其是当它们与我需要的新活动相似时,因为 Eclipse 会为您重命名所有内容。

Otherwise, as others have said, the File->New->Class command works well and will build your file for you including templates for required methods based on your class, its inheritance and interfaces.

否则,正如其他人所说, File->New->Class 命令运行良好,将为您构建文件,包括基于您的类、其继承和接口所需方法的模板。

回答by uday

For creating new Activity simply click ctrl+N one window is appear select android then another window is appear give name to that Secondary Activity.Now another Activity is created

要创建新活动,只需单击 ctrl+N 出现一个窗口,选择 android,然后出现另一个窗口,为该辅助活动命名。现在创建另一个活动

回答by Jason D.

There is no tool, that I know of, which is used specifically create activity classes. Just using the 'New Class' option under Eclipse and setting the base class to 'Activity'.

据我所知,没有专门用于创建活动类的工具。只需使用 Eclipse 下的“新建类”选项并将基类设置为“活动”。

Thought here is a wizard like tool when creating/editing the xml layout that are used by an activity. To use this tool to create a xml layout use the option under 'New' of 'Android XML File'. This tool will allow you to create some of the basic layout of the view.

在创建/编辑活动使用的 xml 布局时,这里是一个类似向导的工具。要使用此工具创建 xml 布局,请使用“Android XML 文件”的“新建”下的选项。此工具将允许您创建视图的一些基本布局。

回答by Ram

I have create a eclipse plugin to create activity in one click .

我已经创建了一个 eclipse 插件来一键创建活动。

Just download the Plugin from https://docs.google.com/file/d/0B63U_IjxUP_GMkdYZzc1Y3lEM1U/edit?usp=sharing

只需从https://docs.google.com/file/d/0B63U_IjxUP_GMkdYZzc1Y3lEM1U/edit?usp=sharing下载插件

Paste the plugin in the dropins folder in Eclipse and restart eclipse

将插件粘贴到Eclipse的dropins文件夹中,重启eclipse

For more details please see my blog
http://shareatramachandran.blogspot.in/2013/06/android-activity-plugin-for-eclispe.html

更多详情请看我的博客
http://shareatramachandran.blogspot.in/2013/06/android-activity-plugin-for-eclispe.html

Need your comment on this if it was helpful...

如果有帮助,需要您对此发表评论...