您的位置:首页 > 移动开发 > Android开发

Android入门学习笔记(四):Activity初步

2011-08-10 01:51 495 查看

Android入门学习笔记(四):Activity初步

.Activity的主要作用

Activity是一个类,是用户和应用程序之间进行交互的接口,Activity是各种控件的容器,类似于一个界面

.创建Activity的方法

1. 一个Activity就是一个类,并继承Activity类或其子类

2. 需要重写onCreate方法并设置定义其布局文件

如setContentView (R.layout.main)指明该Activity的布局文件为layout文件夹中的main.xml

3. 设置Activity的布局,并添加必要的控件

4. 每一个Activity都需要在AndroidManifest.xml中进行配置

.为Activity添加控件的方法

如对应的布局文件main.xml如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
根标签LinearLayout表示其范围内都是是线性布局

在下面注册了一个TextView控件和一个Button控件 如图



如需继续添加控件只需要在这个main.xml中继续添加对应的子标签 ,如 EditText、CheckedBox等

.Activity与AndroidManifest.xml

每一个Activity都需要在AndroidManifest.xml中进行配置

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

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

<activity android:name=".OtherActivity"
android:label="@string/other"
/>

</application>
</manifest>
上篇文章 Android入门学习笔记(三):第一个Android应用程序中已解释了AndroidManifest.xml文件可以配置应用程序的图标、名字、应用程序可兼容的最低SDK版本等,此外,每一个Activity都需要在AndroidManifest.xml中进行配置,配置方法是在<application>
标签中添加子标签 <activity> ,并至少设置好name属性和label属性
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: