第一个Android程序——认识文件结构
通过这个程序,认识Android编程各个文件之间的联系和使用方法,以及认识Activity
文件结构的大体概述见:http://www.cnblogs.com/liushang0419/archive/2011/05/28/2060624.html
该程序任务:添加一个显示文本和一个按钮
对Activity的初步认识:就像一个窗口,能显示信息,又像一个容器,能容纳功能空间,如button,在程序角度上看,又像一个 类,可以和其他的类(Activity)发生联系。
创建Activity的要点:
- 一个Activity就是一个类,类名随意起,不过必须继承Activity这个父类。
- 需要复写onCreate()方法
- 每一个Activity都应该在AndroidManifest.xml文件中进行配置
- 为Activity添加必要的控件
整体文件代码预览:

//MyActivity.java 文件
package geeker.MyActivity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class MyActivity extends Activity {
//成员变量的声明
private TextView myTextView = null;
private Button myButton = null;
//重写OnCreate方法,会自动生成
public void onCreate(Bundle savedInstanceState) {
//调用父类方法,该句代码自动生成
super.onCreate(savedInstanceState);
//通过布局文件的id调用该Activity所使用的布局文件
setContentView(R.layout.main);
//通过findViewById()方法拿到布局文件中添加的控件
//不过在布局文件中添加控件的时候必须定义id号,
//如:android:id="@+id/myTextView"
myTextView = (TextView)findViewById(R.id.myTextView);
myButton = (Button)findViewById(R.id.myButton);
//向控件上制定显示文字
myTextView.setText("This is my first Activity !");
myButton.setText("MY FIRST BUTTON");
}
}
//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"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
//R。jar 文件该文件自动生成,不要自己改动
/* 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 geeker.MyActivity;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=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;
}
}
其实走一遍添加Button的流程就明白各个文件间的联系了:
1 先打开main.xml文件,加一个button按钮的布局
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
2 其实上一步完成后,编译运行已经能看到一个Button按钮了,但是我想在按钮上添加文字以说明该按钮的作用,在java中的程序为 :
Button bt = new Button();
bt.setText("MY FIRST BUTTON");
那么在Android程序中如何在.java源文件中拿到刚才在main.xml中添加的控件呢?
基于这个目的,在main.xml文件中加了此句:android:id="@+id/myButton",这一句使得R.java文件中多了一个叫id的类,该控件的id号就在这个类中出现了,这样做为了方便.java文件中的调用。
实际上如果不加上一句,该控件是不会在R.java文件中产生id号码的,因为只有在res目录中添加文件才会自动在R.java中产生id号,而添加一个控件只是在一个资源文件中做修改而已,所以不会自动产生id号。
我们可以看一下R.java文件中自动产生的ID代码:
public static final class id {
public static final int myButton=0x7f050001;
public static final int myTextView=0x7f050000;
}
然后在.java文件中就可通过getViewById()方法拿到控件了
拿到控件之后就可以像java程序中一样进行相关操作了,代码如:
private Button myButton = null;
myButton = (Button)findViewById(R.id.myButton);
myButton.setText("MY FIRST BUTTON");
实际上这个流程只体现了xml文件和R.java文件之间的联系(通过该句:android:id="@+id/myButton),以及.java与R.java之间的联系(通过该句:findViewById(R.id.myTextView) )
补充一下其他文件关系的代码体现:
MyActivity.java文件与Main.xml文件的联系时通过MyActivity.java文件中的setContentView(R.layout.main);体现的,因为一个Activity文件要对应一个布局文件
MyActivity.java文件与AndroidManifest.xml文件之间的联系时通过AndroidManifest.xml文件中的
<activity android:name=".MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
OVER
PS:主要过去写程序很少涉及多文件结构,所以这次细细总结了一番