David.Ko

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

          使用基本的Widget

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

          第六章 使用基本的 Widget

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

          Lable

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

                      Java中,你可以通過創(chuàng)建一個(gè) TextView的實(shí)例來創(chuàng)建一個(gè) label。更常見的是通過添加一 TextView元素到布局文件來創(chuàng)建一 label,使用一個(gè) android:text屬性設(shè)置 label的值。如果你需要因?yàn)槟承┬枨蟾淖?/span> label的值,比如國際化之類,你可能會(huì)使用 XML的資源引用,這些在本書后面章節(jié)介紹。

          • TextView有許多的和 lable相關(guān)的屬性,例如:
          • android:typeface 用來設(shè)定 label 的字體(例如, monospace
          • android:textStyle 用來設(shè)置字體應(yīng)該是 加黑 ( bold ), 斜體 ( italic ), 或者加黑并且斜體 ( bold_italic )
          • android:textColor 用來設(shè)置 label 文字的顏色,用 RGB 十六進(jìn)制表示(例如: #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?"

          />

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

          Button

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

          使用 Image

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

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

                      ImageButton ImageView的子類,他還有標(biāo)準(zhǔn)按鈕的行為,比如響應(yīng)點(diǎn)擊等。

                      例如, ImageView實(shí)例工程的布局文件:

          <?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"

          />

          結(jié)果如下:

          6. ImageViewDemo示例工程

          Field

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

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

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

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

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

                      例如,從 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 設(shè)置為 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" );

          }

          }

                  結(jié)果如下:

          7. FieldDemo 示例工程

                      注意: Android 模擬器僅能裝載一個(gè) Java 包里面的一個(gè)應(yīng)用程序。因?yàn)楸菊滤械膶?shí)例程序共享 com.commonsware.android.basic 包,所以,你只能在同一時(shí)間運(yùn)行一個(gè)程序。

                      Field 的另一個(gè)特色是他的自動(dòng)補(bǔ)全功能,這個(gè)功能幫助用戶不需要輸入全部的文字。 AutoCompleteTextView widget 提供這個(gè)功能,本書后面會(huì)詳細(xì)討論。

          CheckBox

                      經(jīng)典的 checkbox 有兩種狀態(tài),選中和沒有選中。點(diǎn)擊 checkbox ,會(huì)在這兩種狀態(tài)之間轉(zhuǎn)變。

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

                 Java 里,你可以調(diào)用:

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

          ·    setChecked() 設(shè)置 chechbox 為選中狀態(tài)或者未選中狀態(tài)

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

          你也可以注冊(cè)一 listener 對(duì)象(一個(gè) OnCheckedChangeListener 實(shí)例)來監(jiān)聽 checkbox 狀態(tài)的改變。

          例如,下面是 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" />

                  相應(yīng)的 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" );

          }

          }

          }

                  注意,因?yàn)閷?shí)現(xiàn)了 OnCheckedChangeListener 接口, activity 自己作為 checkbox listener (通過 cb.setOnCheckedChangeListener(this) )。 Listener 的回調(diào)函數(shù)是 onCheckedChanged() ,這個(gè)函數(shù)接收 checkbox 的變化。在本例中,我們用改變 checkbox 的文字信息來反映 checkbox 的變化。

                 結(jié)果就是點(diǎn)擊 checkbox ,他的文字就會(huì)更新,如下所示:

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

          9. 示例工程, checkbox 被選中

          Radio

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

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

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

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

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

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

          例如,在 RadioButton 示例工程中, XML 布局文件展示了一個(gè) 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>

                  運(yùn)行結(jié)果如下:

          10. RadioButtonDemo 示例程序

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

          View

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

          有用的屬性

                  View 類經(jīng)常會(huì)用到的屬性:

          ·    控制焦點(diǎn)的順序:

          ·                   android:nextFocusDown

          ·                   android:nextFocusLeft

          ·                   android:nextFocusRight

          ·                   android:nextFocusUp

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

          ·    android:background ,設(shè)置 widget background

          有用的方法

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

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

                  為了幫助導(dǎo)航樹 widget 和容器,你可以使用:

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

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

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

          主站蜘蛛池模板: 新平| 浮梁县| 新丰县| 涟水县| 炎陵县| 桦川县| 宁蒗| 铜梁县| 祁连县| 曲阳县| 伊宁市| 徐汇区| 营山县| 江油市| 沾益县| 铜川市| 岢岚县| 库尔勒市| 都匀市| 赤峰市| 墨玉县| 富民县| 杭州市| 响水县| 陇川县| 东明县| 穆棱市| 南木林县| 谢通门县| 厦门市| 昌平区| 阳西县| 安图县| 怀化市| 鄂托克前旗| 扎兰屯市| 监利县| 阿克陶县| 沂南县| 福海县| 博湖县|