天天看點

android中view的focus焦點處理

通過isfocusable()這個方法我們可以知道view是否具有接受焦點的資格,通過setfocusable().來設定view接受焦點的資格;

         對應在觸摸模式下,你可以調用isfocusableintouchmode().來獲知是否有焦點來響應點觸,也可以通過setfocusableintouchmode().來設定是否有焦點來響應點觸的資格.

         在activity中,當我們按上下左右方向鍵時,view中的焦點也會移動,我們怎麼控制他的移動順序呢,在xml屬性布局檔案中,有四個屬性: nextfocusdown , nextfocusleft , nextfocusright ,和nextfocusup, 設定他們的值來明确焦點從目前界面移動下個界面的id。例如:

<linearlayout 

    android:orientation="vertical" 

    ... > 

  <button android:id="@+id/topbtn" 

          android:nextfocusup="@+id/bottombtn" //當焦點處在這個topbtn按鈕上,再按向上方向鍵,焦點就會移動到bottombtn按鈕上。

          ... /> 

  <button android:id="@+id/bottombtn" 

          android:nextfocusdown="@+id/topbtn" //當焦點處在這個bottombtn按鈕上,再按向下方向鍵,焦點就會移動到topbtn按鈕上。

</linearlayout>

二.不讓文本輸入框預設獲得焦點,彈出鍵盤占據我們的螢幕的方法

有時會遇到一種情況,在真機運作一個頁面,焦點會預設在edittext上,然後自動彈出鍵盤來占據大半個螢幕。處理方法很簡單。在配置檔案中加一句:

<activity ...  android:windowsoftinputmode="statealwayshidden|adjustresize"  />

這樣即可,意思是預設隐藏鍵盤輸入法。

繼續閱讀