2014-12-19 30 views
0

手動指定下一個視圖的特定方向在android中的方法或配置是什麼?如何在android中手動指定特定方向的下一個視圖ID?

,比如我有三個按鈕:

button1button2button3

通常情況下,當我點擊正確的方向,我想改變重點從button1button2,然後button3,但如果我有一個奇怪的規定:我想將焦點從button1更改爲button3,然後改爲button2

更常見的要求是,當焦點在button3時,我想要將焦點更改爲button1,點擊正確方向時!

我該如何滿足這些要求?

+0

在'onCreate'獲取所有需要的按鈕的視圖引用,並將它們存儲在一個數組中。然後你可以遍歷數組並改變焦點 – Panther 2014-12-19 09:02:10

+0

@Panther我有更簡單的方法嗎?我記得c#有一個名爲'tabindex'的屬性可以控制按下'TAB'時的順序。 – roger 2014-12-19 09:18:29

回答

2

你嘗試過android視圖的nextFocusRight/Left/Up/Down屬性嗎?

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:nextFocusRight="@+id/button2" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:nextFocusRight="@id/button1" /> 

您可以在Java代碼中設置這些屬性。

Button button1 = (Button) findViewById(R.id.button1); 
    Button button2 = (Button) findViewById(R.id.button2); 
    Button button3 = (Button) findViewById(R.id.button3); 

    button3.setNextFocusRightId(R.id.button1); 
相關問題