1. 程式人生 > >Android 和焦點相關的三對方法

Android 和焦點相關的三對方法

常常感到疑惑,和焦點相關的下面三對方法有什麼不同:

isFocusable()
isFocusableInTouchMode()

setFocusable()
setFocusableInTouchMode()

requestFocus()
requestFocusFromTouch()

首先,其他人一定是寫過很多部落格的,但是上網一搜,發現大部分都是非常簡單而且重複的,如果要弄懂,需要浪費大把的時間去分辨究竟誰寫的是正確的、清晰的。其次,官方文件已經對這幾個方法解釋的夠清楚了,只是一直沒有去看文件,今天特地看看,然後做一個筆記。

closed as off-topic by nhaarman, EdChum, Soner Gönül, Jay Patel, Thomas Fenzl May 22 '14 at 8:39 This question appears to be off-topic. The users who voted to close gave this specific reason:

“This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.” – nhaarman, EdChum, Soner Gönül, Jay Patel, Thomas Fenzl If this question can be reworded to fit the rules in the help center, please edit the question.

這個問題被人投票關閉了,因為太…好吧,這個插曲告訴我們,api 相關的問題,應該優先檢視文件

isFocusable() 與 isFocusableInTouchMode()

文件地址

isFocusable: Returns whether this View is currently able to take focus.當前檢視是否能獲取焦點

isFocusableInTouchMode(): When a view is focusable, it may not want to take focus when in touch mode. For example, a button would like focus when the user is navigating via a D-pad so that the user can click on it, but once the user starts touching the screen, the button shouldn’t take focus。當檢視是可聚焦的,它可能不想在觸控模式下聚焦。例如,當用戶通過D-pad(

十字鍵,方向鍵)導航時,按鈕想要聚焦,以便使用者可以點選它,但是一旦使用者開始觸控式螢幕幕,按鈕就不應該聚焦。

小結: isFocusable 表示是否能獲取焦點,isFocusableInTouchMode 表示觸控模式下是否能獲取焦點。那麼在手機上什麼時候是非觸控模式還能獲取焦點呢?通常是 EditText 在調取鍵盤的時候。我們通常在開啟一個搜尋介面的時候,為了不讓搜尋框預設獲取焦點,都會在 EditText 的父佈局中設定 setFocusable 就是這個道理。

setFocusable() 與 setFocusableInTouchMode()

setFocusable(): Set whether this view can receive the focus.Setting this to false will also ensure that this view is not focusable in touch mode.

setFocusableInTouchMode(): Set whether this view can receive focus while in touch mode. Setting this to true will also ensure that this view is focusable.

小結: 有了對 isFocusable() 和 isFocusableInTouchMode() 的理解,這兩個方法還是比較好理解的。如果設定了 setFocusable 為 false , 那麼 View 在觸控模式下也不能獲取焦點。如果設定了 setFocusableInTouchMode 為 true , 那麼 View 就自動能獲取焦點了。這兩就像 Java 中 && || 這兩個關鍵字的效果。很好理解。

requestFocus() 與 requestFocusFromTouch()

requestFocus() : Call this to try to give focus to a specific view or to one of its descendants. A view will not actually take focus if it is not focusable (isFocusable() returns false), or if it can’t be focused due to other conditions (not focusable in touch mode (isFocusableInTouchMode()) while the device is in touch mode, not visible, not enabled, or has no size). 嘗試賦予當前 View 或者當前 View 子節點焦點。如果一個 View 是以下情況,那麼不能獲取焦點:

  • 不能獲取焦點或者在觸控模式下不能獲取焦點,也就是 isFocusable() = false 或者 isFocusableInTouchMode() = false
  • 檢視不可見
  • 檢視不可用
  • 檢視大小為 0

requestFocusFromTouch(): Call this to try to give focus to a specific view or to one of its descendants. This is a special variant of requestFocus() that will allow views that are not focusable in touch mode to request focus when they are touched.

小結: 有前面兩對方法的理解,這一對就非常好理解了。

總結:

API 相關的疑問看文件!!!