Hello,World是咱們每學一門語言,第一個要寫得程序,今天我就從這開始。
打開ECLIPSE,選擇 File > New > Project.>others>Andriod project在出現的對話框裏輸入 Project Name,Application Name,Package Name,Create Activity,選擇ANDROID1.5,點擊FINISH.
項目就建完了,很簡單吧。出乎個人意料。java
如今看看具體代碼文件,在左邊的package explorer,select project helloworld.android
在src下打開HelloAndriod.java文件。app
java代碼:
- package eoe.demo;
- import android.app.Activity;
- import android.os.Bundle;
- public class HelloAndroid extends Activity {
- /** Called when the activity is first created. */
- //程序入口點
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //設置顯示的UI,
- R.layout.main就是一個XML文件。
- }
- }
- /res/layout/main.XML內容以下
- <?xml version="1.0" encoding="utf-8"?>
- <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_;fill_parent"
- android:layout_height="fill_parent"
- android:text="@string/hello"/>
- //在strings.xml定義
- <?xml version="1.0" encoding="utf-8"?>
- //固定的
- xmlns:android=http://schemas.android.com/apk/res/android
- XML的NAMESPACE,每一格都必須有。
複製代碼
android:layout_width
android:layout_heightide
當前VIEW的寬和高。fill_parent的含義在這個程序中是暫居佔據整個SCREEN。text 顯示的內容,這裏採用定義的常量表示。spa
res/values/ strings.xmlcode
java代碼:
xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello, Android! I am a string resource!</string>
- <string name="app_name">Hello, Android</string>
- </resources>
- //順便看一下R.JAVA
- package eoe.demo;
- public final class R {
- public static final class attr { }
- public static final class drawable {
- public static final int icon=0x7f020000;
- }
- 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;
- }
- }
複製代碼