David.Ko

          Follow my heart!
          posts - 100, comments - 11, trackbacks - 0, articles - 0
             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          使用基本的Widget

          Posted on 2009-10-28 14:29 David.Ko 閱讀(1306) 評論(0)  編輯  收藏 所屬分類: Android
          轉載自:http://blog.csdn.net/xieyali/archive/2009/09/15/4555579.aspx

          第六章 使用基本的 Widget

               每一個 GUI工具包都有一些基本的 widget: fields, labels, buttons 等等。 Android 的工具包也不例外,想要了解 widget 如何在 Android Activity 中工作,使用基本 widget 將會是很好的入門。

          Lable

                      最簡單的 widget就是 label,在 Android中就是一個 TextView。和大多數 GUI工具包中的 label一樣, Android label 也是一小段不能被用戶直接修改的文字。通常,他用來表明相鄰的 widget(比如一個 field前面的“ Name: label表明 field需要填寫的內容 )。

                      Java中,你可以通過創建一個 TextView的實例來創建一個 label。更常見的是通過添加一 TextView元素到布局文件來創建一 label,使用一個 android:text屬性設置 label的值。如果你需要因為某些需求改變 label的值,比如國際化之類,你可能會使用 XML的資源引用,這些在本書后面章節介紹。

          • TextView有許多的和 lable相關的屬性,例如:
          • android:typeface 用來設定 label 的字體(例如, monospace
          • android:textStyle 用來設置字體應該是 加黑 ( bold ), 斜體 ( italic ), 或者加黑并且斜體 ( bold_italic )
          • android:textColor 用來設置 label 文字的顏色,用 RGB 十六進制表示(例如: #FF0000 是紅色)

          例如:在 Label 工程中,你可以看到如下的布局文件:

                 <?xml version="1.0" encoding="utf-8"?>

          <TextView xmlns:android="http://schemas.android.com/apk/res/android"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"

          android:text="You were expecting something profound?"

          />

               僅僅這個布局文件,和 Android的工程生成的代碼,可以得到結果如下圖:

          Button

          我們已經在前兩章看到了按鈕 widget的使用。 Button類是 TextView的子類,所以前面幾節討論的一些屬性仍然起作用。

          使用 Image

                      Android有兩種 widget來幫助 Activity嵌入圖片: ImageView ImageButton。顧名思義,可以和 TextView Button類比。

                      每個 widget都有一個 Android:src屬性(在布局文件里)來指定使用那個圖片。圖片通常存放在 drawable資源里,會在后面資源一章詳細描述。你也可以通過使用 content provider setImageURI()方法來設置圖片內容。

                      ImageButton ImageView的子類,他還有標準按鈕的行為,比如響應點擊等。

                      例如, ImageView實例工程的布局文件:

          <?xml version="1.0" encoding="utf-8"?>

          <ImageView xmlns:android="http://schemas.android.com/apk/res/android"

          android:id="@+id/icon"

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          android:adjustViewBounds="true"

          android:src="@drawable/molecule"

          />

          結果如下:

          6. ImageViewDemo示例工程

          Field

                      button label之后, field是多數 GUI工具包的基本元素。在 Android里, field是通過 EditText widget實現的,它是 TextView的子類。

                      除了標準 TextView的屬性(例如, android:textStyle), EditText還有許多有用的屬性,包括:

          ·          android:autoText ,用來控制 field 是否需要拼寫檢查

          • android:capitalize ,用來控制 field 是否自動把輸入單詞的第一個字母大寫
          • android:digits ,用來配置 field 只能輸入某些數字
          • android:singleLine ,用來控制 field 是單行輸入還是多行輸入(例如,回車鍵是移動到下一個 widget 還是添加新的一行)

                 除了這些,你還可以配置 field 使用一些特殊的輸入法,比如 android:numeric 只能輸入數字, android:password 用來屏蔽密碼, android:phoneNumber 輸入電話號碼。如果你想要創建自己的輸入方案(例如:郵編,社保號碼),你需要創建自己的 InputMethod 接口的實現,然后通過 android:inputMethod 配置 field 區使用它。你可以在附錄看到 TourIt 示例工程如果實現。

                      例如,從 Field 工程,下面是布局 XML 文件:

          <?xml version="1.0" encoding="utf-8"?>

          <EditText xmlns:android="http://schemas.android.com/apk/res/android"

          android:id="@+id/field"

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          android:singleLine="false"

          />

                      注意, android:singleLine 設置為 false ,所以用戶可以輸入多行。

                 工程中, Field.java 文件為 Field 填充了一些文字:

                 package com . commonsware . android . basic ;

          import android . app . Activity ;

          import android . os . Bundle ;

          import android . widget . EditText ;

          public class FieldDemo extends Activity {

          @Override

          public void onCreate ( Bundle icicle ) {

          super . onCreate ( icicle );

          setContentView ( R . layout . main );

          EditText fld =( EditText ) findViewById ( R . id . field );

          fld . setText ( "Licensed under the Apache License, Version 2.0 " +

          "(the "" License "" ); you may not use this file " +

          "except in compliance with the License. You may " +

          "obtain a copy of the License at " +

          "http://www.apache.org/licenses/LICENSE-2.0" );

          }

          }

                  結果如下:

          7. FieldDemo 示例工程

                      注意: Android 模擬器僅能裝載一個 Java 包里面的一個應用程序。因為本章所有的實例程序共享 com.commonsware.android.basic 包,所以,你只能在同一時間運行一個程序。

                      Field 的另一個特色是他的自動補全功能,這個功能幫助用戶不需要輸入全部的文字。 AutoCompleteTextView widget 提供這個功能,本書后面會詳細討論。

          CheckBox

                      經典的 checkbox 有兩種狀態,選中和沒有選中。點擊 checkbox ,會在這兩種狀態之間轉變。

                      Android 里,有一個 CheckBox widget 符合你的要求。他以 TextView 作為父類,所以你可以使用 TextView 的屬性,比如 android:textColor 來設置這個 widget

                 Java 里,你可以調用:

          ·    isChecked() 檢查 checkbox 是否被選中

          ·    setChecked() 設置 chechbox 為選中狀態或者未選中狀態

          ·   toggle() 標志 checkbox 被用戶選中

          你也可以注冊一 listener 對象(一個 OnCheckedChangeListener 實例)來監聽 checkbox 狀態的改變。

          例如,下面是 CheckBox 工程的布局文件:

          <?xml version="1.0" encoding="utf-8"?>

          <CheckBox xmlns:android="http://schemas.android.com/apk/res/android"

          android:id="@+id/check"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:text="This checkbox is: unchecked" />

                  相應的 CheckBoxDemo.java 如下所示:

                        public class CheckBoxDemo extends Activity

          implements CompoundButton . OnCheckedChangeListener {

          CheckBox cb ;

          @Override

          public void onCreate ( Bundle icicle ) {

          super . onCreate ( icicle );

          setContentView ( R . layout . main );

          cb =( CheckBox ) findViewById ( R . id . check );

          cb . setOnCheckedChangeListener ( this );

          }

          public void onCheckedChanged ( CompoundButton buttonView ,

          boolean isChecked ) {

          if ( isChecked ) {

          cb . setText ( "This checkbox is: checked" );

          }

          else {

          cb . setText ( "This checkbox is: unchecked" );

          }

          }

          }

                  注意,因為實現了 OnCheckedChangeListener 接口, activity 自己作為 checkbox listener (通過 cb.setOnCheckedChangeListener(this) )。 Listener 的回調函數是 onCheckedChanged() ,這個函數接收 checkbox 的變化。在本例中,我們用改變 checkbox 的文字信息來反映 checkbox 的變化。

                 結果就是點擊 checkbox ,他的文字就會更新,如下所示:

          8. CheckBoxDemo 示例工程, checkbox 沒有選中

          9. 示例工程, checkbox 被選中

          Radio

                  和其他 GUI 工具箱的 Radio 按鈕一樣, Android radio 按鈕有兩種狀態,像 checkbox ,但是在一組的 radio 中,只有一能在同一時間選定。

                  CheckBox 一樣, RadioButton 也是 CompoundButton 的子類,而 CompoundButton TextView 的子類。因此,所有的標準 TextView 的屬性比如 face, style, color 等等都可以用來控制 RadioButton 的外觀。同樣,你可以調用 isChecked() 方法去檢查是否被選中,調用 toggle() 選中他,就像是用 CheckBox 一樣。

                  大多數情況,你會把 RadioButton 放在一個 RadioGroup 里面。 RadioGroup 表明一組 RadioButton 的狀態捆綁在一起,這樣這些 RadioButton 在同一時間內只能由一個被選中。如果你在布局文件里面給你的 RadioButton 設置了 android:id ,你可以通過 Java 代碼來訪問 Group ,同時可以調用:

          ·       Check() ,通過 ID 去檢查一個制定的 Radio 按鈕(例如: group.check(R.id.radio1)

          ·       clearCheck() ,清除一個組里面所有的 Radio 按鈕,所以一個組里面沒有被選中的 RadioButton

          ·       getCheckedRadioButtonId() ,得到當前選中的 RadioButton ID( 沒有選中的返回 -1)

          例如,在 RadioButton 示例工程中, XML 布局文件展示了一個 RadioGroup 中有一組 RadioButton:

          <?xml version="1.0" encoding="utf-8"?>

          <RadioGroup

          xmlns:android="http://schemas.android.com/apk/res/android"

          android:orientation="vertical"

          android:layout_width="fill_parent"

          android:layout_height="fill_parent"

          <RadioButton android:id="@+id/radio1"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:text="Rock" />

          <RadioButton android:id="@+id/radio2"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:text="Scissors" />

          <RadioButton android:id="@+id/radio3"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:text="Paper" />

          </RadioGroup>

                  運行結果如下:

          10. RadioButtonDemo 示例程序

                  注意, RadioButton 組默認狀態是所有的按鈕沒有選中。想要預先設置某個按鈕被選中,在 Activity onCreate 方法中使用 RadioButton setChecked() 方法,或者 RadioGroup check() 方法。

          View

                  所有的 widget ,包括本章已經介紹過的全部 widget 都是 View 的子類,這樣,所有的 widget 就有一組有用的屬性和方法供我們使用。

          有用的屬性

                  View 類經常會用到的屬性:

          ·    控制焦點的順序:

          ·                   android:nextFocusDown

          ·                   android:nextFocusLeft

          ·                   android:nextFocusRight

          ·                   android:nextFocusUp

          ·    android:visibility ,控制 widget 是否可見

          ·    android:background ,設置 widget background

          有用的方法

                  你可以通過 setEnable() 切換 widget 是否可用,通過 isEnable() 方法查看 widget 是否可用。一種常見的使用方式是通過 CheckBox 或者 RadioButton 的選擇來決定某些 widget 是否可用。

                  一個 widget 可以通過 requestFocus() 方法得到焦點,可以通過 isFocused() 方法檢查是否得到焦點。你可以使用這兩個方法和上面的 setEnable() 方法配合,來確定黨僅用一個 widget 時,一個合適的 widget 擁有焦點。

                  為了幫助導航樹 widget 和容器,你可以使用:

                  getParent() ,來找到父 widget 或者容器

          findViewById() ,用一個確定的 id 來找到一個子 widget

                  getRootView() ,得到樹的根(例如:通過 setContentView() 設置的 Activity View

          主站蜘蛛池模板: 会昌县| 清涧县| 梧州市| 原平市| 衡东县| 新宁县| 江门市| 房产| 洪洞县| 广灵县| 新竹县| 宝兴县| 龙岩市| 东丽区| 孝昌县| 体育| 兴安县| 历史| 遂川县| 嵩明县| 光泽县| 开江县| 台东县| 合山市| 南充市| 新乐市| 河津市| 象山县| 汉源县| 宁乡县| 黎川县| 禹州市| 肥东县| 枣强县| 报价| 隆德县| 汕尾市| 桦甸市| 安龙县| 南昌市| 秦皇岛市|