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

Android 应用软件开发(二)Activity

2012年10月11日 ⁄ 综合 ⁄ 共 2737字 ⁄ 字号 评论关闭

Activity是用户与应用程序交互的接口,实际上是一个控件的容器

在Activity中可以摆放很多控件(按钮,文本等)

创建Activity的要点:

1. 一个Activity就是一个类,并且这个类要继承Activity

2. 需要复写onCreate方法,这个方法是当一个Activity第一次运行时由操作系统调用

3. 需要在manifest.xml文件中注册

4. 为Activity添加必要的控件

这个控件放在布局文件中,然后在Activity中调用setContentView使用布局文件

每个控件可以给其制定一个id,然后在Activity中就可以使用findViewById方法得到控件句柄

具体代码如下:

Activity01.java:

package mars.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

/**
 * 创建Activity的要点
 * 1.一个Activity就是一个类,并且这个类要继承Activity
 * 2.需要复写onCreate方法
 * 3.每一个Activity都需要在AndroidManifest.xml文件当中进行配置
 * 4.为Activity添加必要的控件
 * @author Administrator
 *
 */
public class Activity01 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //使用 findViewById方法取得控件句柄
        TextView myView = (TextView)findViewById(R.id.myTextView);
        Button myButton = (Button)findViewById(R.id.myButton);
        //对控件进行相应的操作
        myView.setText("我的第一个View");
        myButton.setText("我的第一个Button");
    }
}

R.java:

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package mars.activity;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class id {
        public static final int myButton=0x7f050001;
        public static final int myTextView=0x7f050000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <Button 
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

string.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, Activity01!</string>
    <string name="app_name">activity01</string>

</resources>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="mars.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".Activity01" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

抱歉!评论已关闭.