Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 開發入門 >> 獲取標識 ID

獲取標識 ID

編輯:開發入門

存取識別符號 
在 上一章談了 XML 描述檔中界面元件的各種「 android: 」開頭的屬性。要使用一個界面元件,第一件事就是定義出界面描述檔。大部分的界面元件 ( 如 LinearLayout 、 TextVIEw) 不需要在程序中作後續處理,因此可以直接描述。不過對於那些將在程序中被參考 (reference) 到的界面元件(如按鈕 Button 、文字輸入欄位 EditText ),我們需要透過在 XML 描述檔中,定義該界面元件的「 android:id 」識別符號屬性。之後在程序中所有與這個界面元件有關的操作,都能根據「 android:id 」 識別符號來在調用這個界面元件。 
代碼 : 
<EditText android:id="@+id/height"    />

 

前面章節提過,寫作時最好將 XML 描述檔屬性分行列出,以易於閱讀( 增加可讀性) 。而我們的范例卻將 android:id 屬性直接擺在 EditText 標簽後。其實這麼做同樣是基於易於閱讀的考量。當然你也可以將「android:id 」屬性分行列出,或是將「android:id 」屬性放在屬性列表的 中間或最後頭,這些作法都是允許的,本書中一律使用將 android:id 屬性直接擺在界面元件標簽後的寫法。

android:id 屬性的內容長得比較特別:

代碼: 
@+id/height

「height 」是這個界面元件的 android:id 。以後的程序中會使用「R.id.height 」來取得這個界面元件。「@+id 」的意思是我們可以通過這個識別符號來控制所對應的界面元件,「R 」類別會自動配置一個位址給這個界面元件。 「R 」類別的內容則可以透過查看 R.Java 得知。

XML 描述檔與 R.Java 檔

在 android 系統中,我們使用 XML 來定義 UI 。但是有些稍微有經驗的開發者可能會有疑問:

「用 XML 來描述界面固然方便,但是對於手機程序來說,直接用 XML 檔桉是不是太占空間了?」。

沒錯,如果 Android 是直接使用 XML 來儲存界面描述到手機上的話,一定會使用比起現在大的多的檔桉空間。解決的方法是Android 並不直接使用 XML 檔桉,而是透過 android 開發工具,自動將 XML 描述檔轉換成資源檔桉。一旦應用程序要使用某個界面或是任何種類的資源( 字串、圖片、圖示、音效...) ,都使用索引來查詢。

 

當你建立一個 BMI 新專桉,打開位於 「src/com/demo/android/bmi 」目錄下的 「R.Java 」檔,你可以看到如下的程序碼:

代碼: 
    /* 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 com.demo.android.bmi;


    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=0x7f040000;

        }

    }

 

在照著前一章新增了 XML 描述後,再次打開打開 「src/com/demo/android/bmi 」 目錄下的 「R.Java 」檔 ,你可以看到如下的程序碼:

代碼: 
/* 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 com.demo.android.bmi;

 

    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 height=0x7f050000;

            public static final int result=0x7f050003;

            public static final int submit=0x7f050002;

            public static final int suggest=0x7f050004;

            public static final int weight=0x7f050001;

        }

        public static final class layout {

            public static final int main=0x7f030000;

        }

        public static final class string {

            public static final int app_name=0x7f040000;

        }

}

 

我們看到在 R.Java 檔桉中,分別有 attr ( 屬性) 、drawable ( 圖片、圖示) 、id ( 識別符號) 、layout ( 界面描述) 、string ( 文字) 這幾種資源型態,就 XML 描述檔中的 id 來說,開發工具會根據 XML 描述檔中指定的 id ,生成對應的資源,並自動指定一個位址。

 

Google 官方文件是這麼解釋「R.Java 」檔案的作用的:

 

A project's R.Java file is an index into all the resources defined in the file. You use this class in your source code as a sort of short-hand way to refer to resources you've included in your project. This is particularly powerful with the code-completion features of IDEs like Eclipse because it lets you quickly and interactively locate the specific reference you're looking for.


The important thing to notice for now is the inner class named "layout", and its member fIEld "main". The Eclipse plugin noticed that you added a new XML layout file and then regenerated this R.java file. As you add other resources to your projects you'll see R.Java change to keep up.

 

有了「R.Java 」做中介,在 XML 描述檔中,我們可以透過

@[ 類型]/[ 識別符號]

這樣的語法來為某個界面元件提供識別符號,以供程序控制。

例如,我們可以用 「@+id/height 」來為對應供輸入身高數字的 EditText 元件提供識別符號。

將字串抽離 XML

當我們在 res 資料夾中新增各種一個 XML 檔桉,或是一張圖片時,開發工具會從 res 資料夾中手機集,並將各種資源整成一個索引,自動產生出 R.Java 檔。透過這個特性,我們可以進一步加工我們的 XML 描述檔,讓界面更易於維護。開啟 res/values/strings.XML ,原始的內容為

代碼: 
<?XML version="1.0" encoding="utf-8"?>

<resources>

<string name="app_name">BMI</string>

</resources>

上面只定義了一個字串「app_name 」,用來表示應用程序名稱(在之後講解 androidManifest.XML 檔桉時將會用到)。 我們看到表示字串的格式為

代碼: 
<string name=" 識別代號"> 文字敘述</string>

我們將上一章中的敘述抽取出來,整理進 strings.XML 檔桉。

完整的 strings.XML 檔桉如下:

代碼: 
<?XML version="1.0" encoding="utf-8"?>

<resources>

<string name="app_name">BMI</string>

    <string name="height"> 身高 (cm)</string>

    <string name="weight"> 體重 (kg)</string>

    <string name="bmi_btn"> 計算 BMI 值</string>

    <string name="bmi_result"> 你的 BMI 值是 </string>

</resources>

 

在 strings.XML 檔桉中,我們在原本的 app_name 字串外,自行定義了另外幾個字串。如果我們再次開啟「R.Java 」檔,我們會發現檔桉中的 string 類別中也自動索引了上面定義好的字串:

代碼: 
public static final class string {

    public static final int app_name=0x7f040000;

        public static final int bmi_btn=0x7f040003;

        public static final int bmi_result=0x7f040004;

        public static final int height=0x7f040001;

        public static final int weight=0x7f040002;

    }

接著,我們把這些字串應用到之前定義好的 XML 描述檔中。透過使用@string/[ 識別符號] 這樣存取 string 類型的格式,來取代 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:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@string/height"

            />

        <EditText android:id="@+id/height"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:numeric="integer"

            android:text=""

            />

        <TextVIEw

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@string/weight"

            />

        <EditText android:id="@+id/weight"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:numeric="integer"

            android:text=""

            />

        <Button android:id="@+id/submit"

     * 
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/bmi_btn"

            />

        <TextVIEw android:id="@+id/result"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text=""

             />

        <TextVIEw android:id="@+id/suggest"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text=""

            />

</LinearLayout>

再次運行 android 模擬器,我們看到與前一節完全相同的界面。但就界面而言,透過將描述字串統一集中在 「string.XML 」 中,我們以後要修改界面時更有彈性了。

至此我們已經完成了 BMI 應用程序負責「顯示 (VIEw) 」的部份


本文來自CSDN博客,轉載請標明出處:http://blog.csdn.Net/onewaveyanlei/archive/2010/04/09/5468441.ASPx#_Toc238891629

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved