手动为android中的特定方向指定下一个视图的方法或配置是什么?
例如我有三个按钮:
button1
button2
button3
通常情况下,当我点击向右方向时,我想将焦点从button1
更改为button2
,然后更改button3
,但如果我有一个奇怪的要求:我想改变焦点来自button1
到button3
,然后转到button2
。
更常见的要求是,当焦点位于button3
时,我希望在点击正确方向时将焦点更改为button1
!
我怎样才能满足这些要求?
答案 0 :(得分:2)
您是否尝试过android view&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);