Android界面設(shè)計(jì)基礎(chǔ):控件焦點(diǎn)4個(gè)步驟
Android設(shè)備有多種多樣,操縱界面也有所不同,比如有觸摸屏、軌跡球,傳統(tǒng)的手機(jī)鍵盤等,因此開(kāi)發(fā)者需要更好地了解,當(dāng)用戶在應(yīng)用程序界面中的不同控件間移動(dòng)時(shí),各個(gè)控件的獲得焦點(diǎn)和失去焦點(diǎn)的順序,以及如何根據(jù)用戶的操作習(xí)慣去自定義這些順序。
一般情況下,Android對(duì)于特定的布局界面,會(huì)自動(dòng)得出一個(gè)合適的控件焦點(diǎn)順序,很多情況下是足夠用的了。但是在有的情況下是有例外的??丶南乱粋€(gè)焦點(diǎn)會(huì)到達(dá)哪一個(gè)控件,主要是判斷當(dāng)前控件在指定的方向布局上(up/down/left/right),哪一個(gè)是最領(lǐng)近的控件,其掃描順序?yàn)閺淖蟮接?,從上到下,就象平時(shí)閱讀書(shū)籍一樣。
然而,這種順序有時(shí)會(huì)帶來(lái)一點(diǎn)小問(wèn)題,比如當(dāng)控件都布置在屏幕的上方時(shí),如果用戶再按“up”鍵,則不會(huì)有任何效果,同樣,當(dāng)控件都在屏幕下方、左邊、右邊時(shí),此時(shí)再按如“down”、“Left”,“Right”鍵時(shí)都不會(huì)再獲得控件的焦點(diǎn)。
在本文的例子中,將講解如何修改默認(rèn)的控件焦點(diǎn)順序,以定制特定的控件切換順序,例子中,多個(gè)按鈕以一個(gè)圓形進(jìn)行了排列,例子可以在
http://android-mt-tutorials.googlecode.com/svn/trunk/SimpleFocus中下載。
步驟1 定義界面布局
我們先設(shè)計(jì)出界面的布局,代碼如下,使用的是Relative相對(duì)布局:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button
- style="@style/clockFaceNum"
- android:text="12"
- android:id="@+id/button12"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="11"
- android:id="@+id/button11"
- android:layout_below="@+id/button12"
- android:layout_toLeftOf="@+id/button12">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="1"
- android:id="@+id/button1"
- android:layout_below="@+id/button12"
- android:layout_toRightOf="@+id/button12">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="10"
- android:id="@+id/button10"
- android:layout_below="@+id/button11"
- android:layout_toLeftOf="@+id/button11">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="2"
- android:id="@+id/button2"
- android:layout_below="@+id/button1"
- android:layout_toRightOf="@+id/button1">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="9"
- android:id="@+id/button9"
- android:layout_below="@+id/button10"
- android:layout_toLeftOf="@+id/button10">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="3"
- android:id="@+id/button3"
- android:layout_below="@+id/button2"
- android:layout_toRightOf="@+id/button2">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="8"
- android:id="@+id/button8"
- android:layout_below="@+id/button9"
- android:layout_toRightOf="@+id/button9">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="4"
- android:id="@+id/button4"
- android:layout_below="@+id/button3"
- android:layout_toLeftOf="@+id/button3">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="7"
- android:id="@+id/button7"
- android:layout_below="@+id/button8"
- android:layout_toRightOf="@+id/button8">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="5"
- android:id="@+id/button5"
- android:layout_below="@+id/button4"
- android:layout_toLeftOf="@+id/button4">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="6"
- android:id="@+id/button6"
- android:layout_below="@+id/button5"
- android:layout_centerHorizontal="true">
- </Button>
- </RelativeLayout>
上面定義的style文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style
- name="clockFaceNum">
- <item
- name="android:layout_width">38dp</item>
- <item
- name="android:layout_height">38dp</item>
- <item
- name="android:onClick">numClicked</item>
- <item
- name="android:textSize">9sp</item>
- </style>
- </resources>
運(yùn)行后,效果如下圖:
步驟2 默認(rèn)的控件焦點(diǎn)切換順序
比如當(dāng)用戶將控件焦點(diǎn)點(diǎn)在12號(hào)按鈕時(shí),點(diǎn)往下的“down”按鈕,默認(rèn)的控件焦點(diǎn)切換順序如下圖:
也就是說(shuō),當(dāng)在按鈕12上往下按的時(shí)候,控件的焦點(diǎn)會(huì)切換到11,接著就是鍵10,如此類推。
步驟3 創(chuàng)建自定義的控件焦點(diǎn)順序
下面,我們嘗試創(chuàng)建自定義的控件焦點(diǎn)順序,即同時(shí)允許在上面的界面中,當(dāng)用戶按鍵時(shí),以順時(shí)針或逆時(shí)針進(jìn)行控件切換,如下圖:
也就是說(shuō),允許用戶當(dāng)按“Down”或“Right”鍵時(shí),切換順序是順時(shí)針?lè)较?,比如假設(shè)當(dāng)前在鍵12上,按“Down”或“Right”鍵時(shí),會(huì)切換到鍵1,而按“Up”或”Left”時(shí),會(huì)切換到鍵11,如此類推。要實(shí)現(xiàn)這點(diǎn),可以在每個(gè)按鈕中進(jìn)行設(shè)置如下四個(gè)屬性:
android:nextFocusUp- 定義當(dāng)點(diǎn)up鍵時(shí),哪個(gè)控件將獲得焦點(diǎn)
android:nextFocusDown-定義當(dāng)點(diǎn)down鍵時(shí),哪個(gè)控件將獲得焦點(diǎn)
android:nextFocusLeft-定義當(dāng)點(diǎn)left鍵時(shí),哪個(gè)控件將獲得焦點(diǎn)
android:nextFocusRight--定義當(dāng)點(diǎn)right鍵時(shí),哪個(gè)控件將獲得焦點(diǎn)
下面是其代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button
- style="@style/clockFaceNum"
- android:text="12"
- android:id="@+id/button12"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:nextFocusUp="@+id/button11"
- android:nextFocusLeft="@+id/button11"
- android:nextFocusRight="@+id/button1"
- android:nextFocusDown="@+id/button1">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="11"
- android:id="@+id/button11"
- android:layout_below="@+id/button12"
- android:layout_toLeftOf="@+id/button12"
- android:nextFocusUp="@+id/button10"
- android:nextFocusLeft="@+id/button10"
- android:nextFocusRight="@+id/button12"
- android:nextFocusDown="@+id/button12">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="1"
- android:id="@+id/button1"
- android:layout_below="@+id/button12"
- android:layout_toRightOf="@+id/button12"
- android:nextFocusUp="@+id/button12"
- android:nextFocusLeft="@+id/button12"
- android:nextFocusRight="@+id/button2"
- android:nextFocusDown="@+id/button2">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="10"
- android:id="@+id/button10"
- android:layout_below="@+id/button11"
- android:layout_toLeftOf="@+id/button11"
- android:nextFocusUp="@+id/button9"
- android:nextFocusLeft="@+id/button9"
- android:nextFocusRight="@+id/button11"
- android:nextFocusDown="@+id/button11">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="2"
- android:id="@+id/button2"
- android:layout_below="@+id/button1"
- android:layout_toRightOf="@+id/button1"
- android:nextFocusUp="@+id/button1"
- android:nextFocusLeft="@+id/button1"
- android:nextFocusRight="@+id/button3"
- android:nextFocusDown="@+id/button3">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="9"
- android:id="@+id/button9"
- android:layout_below="@+id/button10"
- android:layout_toLeftOf="@+id/button10"
- android:nextFocusUp="@+id/button8"
- android:nextFocusLeft="@+id/button8"
- android:nextFocusRight="@+id/button10"
- android:nextFocusDown="@+id/button10">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="3"
- android:id="@+id/button3"
- android:layout_below="@+id/button2"
- android:layout_toRightOf="@+id/button2"
- android:nextFocusUp="@+id/button2"
- android:nextFocusLeft="@+id/button2"
- android:nextFocusRight="@+id/button4"
- android:nextFocusDown="@+id/button4">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="8"
- android:id="@+id/button8"
- android:layout_below="@+id/button9"
- android:layout_toRightOf="@+id/button9"
- android:nextFocusUp="@+id/button7"
- android:nextFocusLeft="@+id/button7"
- android:nextFocusRight="@+id/button9"
- android:nextFocusDown="@+id/button9">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="4"
- android:id="@+id/button4"
- android:layout_below="@+id/button3"
- android:layout_toLeftOf="@+id/button3"
- android:nextFocusUp="@+id/button3"
- android:nextFocusLeft="@+id/button3"
- android:nextFocusRight="@+id/button5"
- android:nextFocusDown="@+id/button5">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="7"
- android:id="@+id/button7"
- android:layout_below="@+id/button8"
- android:layout_toRightOf="@+id/button8"
- android:nextFocusUp="@+id/button6"
- android:nextFocusLeft="@+id/button6"
- android:nextFocusRight="@+id/button8"
- android:nextFocusDown="@+id/button8">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="5"
- android:id="@+id/button5"
- android:layout_below="@+id/button4"
- android:layout_toLeftOf="@+id/button4"
- android:nextFocusUp="@+id/button4"
- android:nextFocusLeft="@+id/button4"
- android:nextFocusRight="@+id/button6"
- android:nextFocusDown="@+id/button6">
- </Button>
- <Button
- style="@style/clockFaceNum"
- android:text="6"
- android:id="@+id/button6"
- android:layout_below="@+id/button5"
- android:layout_centerHorizontal="true"
- android:nextFocusUp="@+id/button5"
- android:nextFocusLeft="@+id/button5"
- android:nextFocusRight="@+id/button7"
- android:nextFocusDown="@+id/button7">
- </Button>
- </RelativeLayout>
下圖中是假定在鍵12開(kāi)始按down鍵時(shí)的焦點(diǎn)切換順序:
步驟4 設(shè)置界面的初始控件焦點(diǎn)
在每個(gè)頁(yè)面加載時(shí),可以設(shè)置界面中初始的控件焦點(diǎn),以方便用戶的定位操作,只需要在控件中加入即可。比如:
- <Button
- style="@style/clockFaceNum"
- android:text="12"
- android:id="@+id/button12"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:nextFocusUp="@+id/button11"
- android:nextFocusLeft="@+id/button11"
- android:nextFocusRight="@+id/button1"
- android:nextFocusDown="@+id/button1">
- <requestFocus />
- </Button>
小結(jié)
作為開(kāi)發(fā)者,一定要記住由于Android設(shè)備的多樣性,用戶如何在界面上方便地進(jìn)行輸入或在不同的控件中來(lái)回切換是十分重要的,本文簡(jiǎn)單介紹了用戶如何自定義控件的焦點(diǎn)切換順序,這對(duì)于用戶界面的體驗(yàn)是很有好處的。