Android ApiDemos樣本解析(115):Views->Focus->1. Ver

來源:互聯網
上載者:User

Android UI系統一般可以根據使用者輸入(方向鍵或是Track ball)在不同的UI控制項中移動“焦點(Focus)”。並可以根據UI的顯示或隱藏知道更換當前帶有Focus的控制項。而View可以通過isFocusable 傳回值來確定它是否可以接受Focus。View調用setFocus 來決定它是否願意接受Foucs. 而在支援Touchable 模式下,使用對應的isFocusableInTouchMode 和setFocusableInTouchMode方法。

預設的移動Foucs的演算法是尋找指定方向最近的一個UI控制項。可以通過nextFocusDown, nextFocusLeft, nextFocusRight, 和 nextFocusUp來修改Focus預設移動演算法。nextFocusDown, nextFocusLeft, nextFocusRight, 和 nextFocusUp明確指定了不同方向上移動Focus的下一個UI控制項。

本例使用垂直方向排列UI控制項:並把WebView的focusable 設為false,沒有使用nextFocusDown 和 nextFocusUp ,因此會使用Android預設移動Focus的演算法。

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:orientation=”vertical”>

<TextView android:id=”@+id/txtStatus”
android:text=”@string/focus_1_message”
android:layout_height=”wrap_content”
android:layout_width=”wrap_content” />

<ListView android:id=”@+id/rssListView”
android:background=”#7700CC00″
android:layout_height=”wrap_content”
android:layout_width=”match_parent” />

<WebView android:id=”@+id/rssWebView”
android:background=”#77CC0000″
android:layout_height=”50dip”
android:layout_width=”match_parent”
android:focusable=”false” />

<Button android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:text=”@string/focus_1_placeholder” />

</LinearLayout>

本例使用四種UI控制項:

TextView 預設情況無法接受Focus,如果需要接受Foucs,可以設定android:focusable=”true”.
ListView 含三個清單項目Ars Technica”, “Slashdot”, “GameKult”,可以接受Focus
WebView 將其 android:focusable=”false”,理論上通過方向鍵移動Focus時會跳過此View,但好像對WebView不起作用,也不知道是不是Android的Bug。
Button, 預設可以接受Focus。


作者:mapdigit

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.