Android的 - 在Eclipse中创建一个新的活动 [英] Android - Creating a new activity in Eclipse

查看:138
本文介绍了Android的 - 在Eclipse中创建一个新的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

容易的。

我已经通过了一些指南和教程走了,他们如何开始一个活动(意图)相当明确。

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

不过,我如何在Eclipse中创建一个新的活动?我大概可以做到这一点的手,但后来我不得不修改研究文件,该文件是自动生成的,并添加一个新的XML布局。

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

推荐答案

确定。作为一个新手,我自己,我认为上述两个答案都在思考太多。他问得很干脆如何在Eclipse中创建一个新的活动。我想这就是他想要的:

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 Activity in Eclipse is actually a Class.

您会双击左边的Package ExplorerSRC,然后突出显示你的玉米。名,单击右键,选择新建,然后选择类。输入名称 NewActivity ,并设置超类 android.app.Activity ,然后点击完成。

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 Name as NewActivity and set the Superclass to android.app.Activity, then hit Finish.

在NewActivity.java文件打开就应该是这样的:

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

package com.example.yourappname;

import android.app.Activity;

public class NewActivity extends Activity {

}

您可以将超类空白并添加延伸活动到code本身,如果你preFER。

You can leave the Superclass blank and add extends Activity to the code itself if you prefer.

最后一步是添加活动到您的清单。所以AndroidManifest.xml中双击打开它,然后单击底部的应用程序选项卡。旁边的应用程序节点中,单击添加。突出活动(用大写字母A的方框),然后单击确定。看看现在的'属性的活动框,然后输入一个名称为一个周期的活动和precede它。在这个例子中,你会进入.NewActivity。

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'.

然后你可以添加你的的onCreate() code,所以它看起来是这样的:

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

public class NewActivity extends Activity {

     @Override
     public void onCreate(Bundle savedInstanceState) {         

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

main_view 将是你的主要观点xml文件, main_view.xml ,你将创造在布局目录

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

要调用新的活动,你的意图在code(在不同的活动),以启动一个新的活动看起来是这样的:

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

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

就是这样,你有code调用新的活动,你创造了它。我希望这可以帮助别人。

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

这篇关于Android的 - 在Eclipse中创建一个新的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆