Android - 在 Eclipse 中创建新活动

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

Android - Creating a new activity in Eclipse

androideclipse

提问by Hein du Plessis

Easy one.

简单的一个。

I've gone through a few guides and tutorials, and they're quite clear on how to start an activity (with intent).

我已经阅读了一些指南和教程,它们非常清楚如何开始一项活动(有意)。

However, how do I create a new activity in Eclipse? I can probably do this by hand, but then I have to modify the Rfile, which is auto-generated, and add a new xml layout.

但是,如何在 Eclipse 中创建新活动?我可能可以手动完成此操作,但随后我必须修改R自动生成的文件,并添加新的 xml 布局。

回答by ShadowGod

Ok. Being a newbie myself I think the above two answers are thinking too much. He's asking very simply how to create a new activity in Eclipse.. I think this is what he wants:

好的。作为一个新手,我觉得上面两个答案想的太多了。他问的很简单,如何在 Eclipse 中创建一个新活动。我认为这就是他想要的:

A new Activityin Eclipse is actually a Class.

ActivityEclipse 中的 new实际上是一个Class.

You would doubleclick 'src' on the left side in the Package Explorer, then highlight your 'com.' name, right click, select 'New' and then select 'Class'. Enter the Nameas NewActivityand set the Superclassto android.app.Activity, then hit Finish.

您可以双击 Package Explorer 左侧的“src”,然后突出显示您的“com”。名称,右键单击,选择“新建”,然后选择“类”。输入NameasNewActivity并将其设置Superclassandroid.app.Activity,然后点击完成。

When the NewActivity.java file opens up it should look like this:

当 NewActivity.java 文件打开时,它应该是这样的:

package com.example.yourappname;

import android.app.Activity;

public class NewActivity extends Activity {

}

You can leave the Superclassblank and add extends Activityto the code itself if you prefer.

如果您愿意,您可以将其Superclass留空并添加extends Activity到代码中。

The final step is adding the Activity to your Manifest. So doubleclick AndroidManifest.xml to open it up and then click the 'Application' tab on the bottom. Next to the 'Application Nodes' box, click 'Add'. Highlight 'Activity' (the square box with a capital A) and click 'Ok'. Now look for the 'Attributes for Activity' box and enter a Name for the Activity and precede it by a period. In this example you'd enter '.NewActivity'.

最后一步是将活动添加到您的清单中。因此,双击 AndroidManifest.xml 将其打开,然后单击底部的“应用程序”选项卡。在“应用程序节点”框旁边,单击“添加”。突出显示“活动”(带有大写字母 A 的方框)并单击“确定”。现在查找“活动属性”框并输入活动的名称并在其前面加上句点。在本例中,您将输入“.NewActivity”。

And then you can add your onCreate()code so it looks like this:

然后你可以添加你的onCreate()代码,看起来像这样:

public class NewActivity extends Activity {

     @Override
     public void onCreate(Bundle savedInstanceState) {         

        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main_view);
        //rest of the code
    }
}

main_viewwould be your main view xml file, main_view.xml, that you would create in your layout directory.

main_view将是您main_view.xml将在布局目录中创建的主视图 xml 文件。

To call the new Activity, your Intentin the code (in a different Activity) to start a new Activitylooks something like this:

要调用新的 Activity,您Intent在代码中(在不同的 Activity 中)开始一个新的Activity看起来是这样的:

Intent startNewActivityOpen = new Intent(PresentActivity.this, NewActivity.class);
startActivityForResult(startNewActivityOpen, 0);

And that's it, you have the code to call the new activity and you created it. I hope this helps someone.

就是这样,您拥有调用新活动的代码并创建了它。我希望这可以帮助别人。

回答by Indigenuity

I know this is an old question, but I know there are still people with this same question(I did up until today)

我知道这是一个老问题,但我知道还有人有同样的问题(我一直到今天)

If you add a new activity to your manifest file, there's a special link to click on to automatically create the new Activity, complete with the onCreate() method ready to be filled in.

如果您将新活动添加到清单文件中,则会有一个特殊链接供您单击以自动创建新活动,并准备好填写 onCreate() 方法。

Open the AndroidManifest.xml, and go to the 'Application' tab. Under 'Application Nodes', find and click the 'Add' button. You will likely create a new element at the top level, so select that option, highlight 'Activity', and press OK.

打开 AndroidManifest.xml,然后转到“应用程序”选项卡。在“应用程序节点”下,找到并单击“添加”按钮。您可能会在顶层创建一个新元素,因此选择该选项,突出显示“活动”,然后按“确定”。

Once you've created the Activity, go to the 'Attributes for Activity' and fill in the name. Once you've filled in the name you want, click on the blue 'Name*' link next to the field. The new file wizard will show up, and all you have to do is press OK.

创建活动后,转到“活动的属性”并填写名称。填写您想要的名称后,单击该字段旁边的蓝色“名称*”链接。将显示新文件向导,您只需按 OK。

Voila! New Activity, registered in the manifest and as a ready-to-go Java class.

瞧!新活动,在清单中注册并作为一个随时可用的 Java 类。

回答by Flash

You create the activity by extending the activity class . Once you have creatd the activity class , You need to add the activity in the androidmanifest file specifying the properties for the activity...

您可以通过扩展活动类来创建活动。创建活动类后,您需要在 androidmanifest 文件中添加活动,指定活动的属性...

A sample one would be something like this ...

一个示例将是这样的......

<activity android:name=".JsonActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

The action here indicates that it is the one that starts first ..

这里的动作表示它是第一个开始的..

I dont think you need to modify the R.java file ... Once you add these in the android manifest file and save it automatically gets updated. Also the things that u added like the layouts, menus, strings, id's etcc.... in the various xml files also get automatically updated...

我不认为你需要修改 R.java 文件......一旦你在 android manifest 文件中添加这些并保存它会自动更新。还有你添加的东西,比如布局、菜单、字符串、id 等......在各种 xml 文件中也会自动更新......

Correct me if i am wrong ...

如果我错了,请纠正我...

回答by Argus9

I tried searching for this question on Google and haven't seen this solution yet, so I thought I'd post it here.

我试着在谷歌上搜索这个问题,但还没有看到这个解决方案,所以我想我会把它贴在这里。

In Eclipse, you can click on the "New" button on the toolbar. Under Android, select Android Activity, and run through the wizard. This is the best solution by far, since it lets you set up a layout and an Activity all in one, while also updating the Manifest for you.

在 Eclipse 中,您可以单击工具栏上的“新建”按钮。在 Android 下,选择 Android Activity,然后运行向导。这是迄今为止最好的解决方案,因为它可以让您将布局和活动合二为一,同时还可以为您更新清单。

回答by Mohamed Lamine

How to add New Activity Eclipse step by Step:

如何逐步添加新活动 Eclipse:

  1. Stpe1:Double click on the androidManifest
  2. Step2:on the Menu bar click Aplication
  3. Step3:Scroll down to application node and CLick add button
  4. Step 4:click select Activity and Ok
  5. step 5:clik on the the Texte(Name* Note:make sur u clik on the texte not into the textbox )
  6. step6:there a new Java Class dialog
    ## Heading ##write the classe name ## Heading ## check checkbox construct from the super classe and and ok..
  1. 步骤1:双击androidManifest
  2. Step2:在菜单栏点击应用
  3. 步骤 3:向下滚动到应用程序节点并单击添加按钮
  4. 第 4 步:点击选择活动和确定
  5. 第 5 步:点击 Texte(名称* 注意:在 texte 上点击不要进入文本框)
  6. 第6步:有一个新的Java类对话框
    ##标题##写类名称##标题##检查来自超类的复选框构造,然后确定..

回答by FixerMark

There is also the tried and tested method of starting with one of the samples and going from there.

还有一种久经考验的方法,从其中一个样本开始,然后从那里开始。

The Hello tutorial is as good a starting point is any, just select the create from existing sample option.

Hello 教程是一个很好的起点,只需选择从现有示例选项创建即可。

The latest update to the eclipse plugin even includes a tool to rename your package should you change your mind though I haven't used it yet so can't say if it works. (Right click on the package then select Android Tools, Rename Application Package).

eclipse 插件的最新更新甚至包括一个工具来重命名你的包,如果你改变主意,虽然我还没有使用它,所以不能说它是否有效。(右键单击包,然后选择 Android 工具,重命名应用程序包)。

回答by FixerMark

It is important to say that if you type the desired name for the new Activity on Name box, a dot must be put before the new name. Otherwise, the window to complete the creation of Java code will not open when you click on names link.

重要的是,如果您在名称框中为新活动键入所需的名称,则必须在新名称之前放置一个点。否则,当您单击名称链接时,完成 Java 代码创建的窗口将不会打开。