編輯:關於 Android 開發
對於剛接觸 Android 的開發者而言,第一個程式往往就是 HelloWorld。以下就來逐一解析這個入門範例所涉及的幾個檔案。
public class HelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
它繼承了 Activity。可以把一個 Activity 想像成手機的螢幕,用於展示一屏的內容,因此所有要顯示內容的螢幕都必須繼承 Activity 才能實現。接著它覆蓋了 onCreate() 方法來對該 Activity 進行初始化,並以 setContentView(R.layout.main) 指定使用 main.xml 這個版面配置檔作為當前 Activity 的內容。main.xml 放在 res/layout 目錄下,我們可以直接用 R.layout.main 來引用它,而無需再透過 File 類別去讀取——只要把 XML 檔的索引交給 Android,它就會自動幫我們找到並使用,這正是 Android 相當便利之處。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
內容不多,開頭是一個 LinearLayout 元件的定義,並在其中放入一個用於顯示文字的 TextView。幾個參數說明如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloWorld!</string>
<string name="app_name">HelloWorld</string>
</resources>
顧名思義,這裡定義了一組組鍵值對,供其他地方使用(例如 main 中對 hello 的引用)。這種做法對於字串的统一管理與國際化(i18n)有著重要意義。
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.flysnow"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloWorld"
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>
<uses-sdk android:minSdkVersion="8" />
</manifest>
這是專案的重要設定檔,諸如 Activity、權限、Intent 等都在此配置。各標籤含義如下:
Android 接入微信支付完全解析:從帳號申請到調起支付的每一步
Android 接入微信支付完全解析:從帳號申請到調起支付的每一步 今天來聊聊 Android 接入微信支付。肯定有人要說:這多簡單啊,還在這裡扯什麼? 那我還真不服。請
Android 逆向基礎:Dex 文件格式解析
Android 逆向基礎:Dex 文件格式解析Dex 是 Android 中可執行的 Dalvik 位元組碼檔案,也是 APK 逆向分析中非常重要的格式。理解 Dex 的
Android 事件分發機制:從 Activity 到 View 的完整流程
Android 事件分發機制:從 Activity 到 View 的完整流程 Android 事件分發經常出現在面試與複雜交互開發中。當使用者點擊屏幕時,事件會從 Act
Android EditText 密碼框明文與密文切換的兩種實現方式
Android EditText 密碼框明文與密文切換的兩種實現 在做登錄界面時,密碼框需要支持「明文/密文」的即時切換,這是很實用的交互細節。下面我整理了布局文件以及