现在的位置: 首页 > 综合 > 正文

Hello Activity

2011年05月10日 ⁄ 综合 ⁄ 共 1951字 ⁄ 字号 评论关闭

HelloActivity 是Android SDK中自带的一个最简单的例子,导入到Eclipse后结构如图

下面分析下几个包含的重点文件

1)AndroidManifest.xml

代码

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package
="com.example.android.helloactivity">
    
<application android:label="Hello, Activity!">
        
<activity android:name="HelloActivity">
            
<intent-filter>
                
<action android:name="android.intent.action.MAIN"/>
                
<category android:name="android.intent.category.LAUNCHER"/>
            
</intent-filter>
        
</activity>
    
</application>
</manifest>

 

这是Android的描述文件,有点类似Java 中的web.xml ,用来定于应用中的内容和行为。

在这个例子中的application节点,用"android:label"属性定义了应用的名称为"Hello,Activity!",运行后会显示在标题栏。

技巧

用"android:icon"属性还可以为应用指定一个图标

activity节点则定义了应用中要使用的activity,类的名字为"HelloActivity"

intent-filter节点则定义了该activity在什么情况下显示,可以定义多个intent过滤器

其中 <action android:name="android.intent.action.MAIN" /> 与<category android:name="android.intent.category.LAUNCHER" />的组合,表明该activity将在应用程序加载器中显示,是应用程序的入口。

2) hello_activity.xml

HelloActivity 的布局文件

代码
<EditText xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"
    android:layout_width
="fill_parent"
    android:layout_height
="fill_parent"
    android:textSize
="18sp"
    android:autoText
="true"
    android:capitalize
="sentences"
    android:text
="@string/hello_activity_text_text" />

 其中“android:layout_width”指定了文本编辑框的宽度为"fill_parent"表示占据整个父容器的宽,"android:layout_height"属性指定了文本编辑框的高度为"fill_parent",还有“android:text” 指定了文本编辑框的内容,这里为"@string/hello_activity_text_text”表示是引用来自文本资源的“hello_activity_text_text”节点的值,见/res/values/strings.xml

3)R.java

"R.java"是一个所有资源文件的索引。在程序中可以通过“R”这个类来访问项目中的资源文件,每当在项目中添加新的资源文件时,你可以看到“R.java”的内容也在自动更新。

3)程序主文件

HelloActivity类,该应用程序的入口, 继承自android.app.Activity

代码
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);

        // Set the layout for this activity.  You can find it
        
// in res/layout/hello_activity.xml
        setContentView(R.layout.hello_activity);
    }

 

 

 

抱歉!评论已关闭.