Android之第一行代碼Helloworld
創建 Android 應用
第一步是通過Eclipse IDE來創建一個簡單地Android應用程序。按照選項File -> New -> Project,最後從嚮導列表選擇 Android New Application 。現在,使用如下的窗口嚮導將應用程序命名爲HelloWorld:
接下來,按照所提供的說明,保持所有的默認輸入,直到最後一步。一旦項目創建成功,將會看到如下的項目界面
Android應用程序剖析
在運行應用之前,需要知道Android項目中的一些文件目錄和文件
- src:包含項目中所有的.java源文件,默認情況下,它包括一個 MainActivity.java源文件對應的活動類,當應用程序通過應用圖標啓動時,將運行它。
- gen:這包含由編譯器生成的.R文件,引用了所有項目中的資源。該文件不能被修改。
- bin:這個文件夾包含Android由APT構建的.apk包文件,以及運行Android應用程序所需要的其他所有東西。
- res/drawable-hdpi:這個目錄下包括所有的爲高密度屏幕設計所需的drawable對象。
- res/layout:這個目錄存放用於定義用戶界面的文件。
- res/values:這個目錄存放各種各樣的包含一系列資源的XML文件,比如字符串和顏色的定義。
- AndroidManifest.xml:這個是應用程序的清單文件,描述了應用程序的基礎特性,定義它的各種組件。
主要活動文件
主要活動代碼在MainActivity.java的Java文件中。這是實際的應用程序文件,將被轉化爲Dalvik可執行文件並運行。
默認生成如下代碼:
package com.example.helloworld;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
注意:R.layout.activity_main引用自res/layout目錄下的activity_main.xml文件。onCreate()是活動被加載之後衆多被調用的方法之一。
Manifest文件
無論開發什麼組件用作應用程序中的一部分,都需要在應用程序項目根目錄下的manifest.xml文件中聲明所有的組件。這個文件是Android操作系統與你的應用程序之間的接口,因此,如果沒有在這個文件中聲明組件,將無法被操作系統所識別。
默認生成的清單如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
…標籤之間是應用程序相關的組件。andnroid:icon屬性指出位於res/drawable-hdpi下面的應用程序圖標。這個應用使用drawable文件夾下名爲ic_launcher.png的圖片。
標籤用於指定一個活動,android:name屬性指定一個Activity類子類的全名。android:label屬性指定用於活動名稱的字符串。可以使用標籤來指定多個活動。
意圖過濾器的action被命名爲android.intent.action.MAIN,表明這個活動被用做應用程序的入口。意圖過濾器的category被命名爲android.intent.category.LAUNCHER,表明應用程序可以通過設備啓動器的圖標來啓動。
@string指的是strings.xml(將在後面介紹)。因此,@string/app_name指的是定義在strings.xml中的app_name,實際爲"Hello World"。類似的,應用中的其他字符串也很流行。
Strings 文件
strings.xml文件在res/value文件夾下,它包含應用程序使用到的所有文本。例如,按鈕、標籤的名稱,默認文本,以及其他相似的strings。這個文件爲他們的文本內容負責。默認的strings文件看起來如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Helloworld</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>
R文件
gen/com.example.helloworld/R.java文件是活動的Java文件,如MainActivity.java的和資源如strings.xml之間的膠水。這是一個自動生成的文件,不要修改R.java文件的內容。
Layout 文件
activity_main.xml是一個在res/layout目錄下的layout文件。當應用程序構建它的界面時被引用。將非常頻繁的修改這個文件來改變應用程序的佈局。在"Hello World"應用程序中,這個文件具有默認的佈局。
運行應用程序
運行剛剛建立的Hello World!應用程序。
注意:假設在搭建環境時已經創建好了AVD。
參考於:https://www.runoob.com/android/android-hello-world-example.html