我正在搜索一个在app中导航的方法,用虚拟dpad抛出所有元素。但是如何在上,下,左,右方向找到最近邻居? 例如,我收到一个事件,现在我想把焦点放在下面的按钮上。
我不想在xml文件中使用这样的定义:
<LinearLayout android:orientation="vertical" ... >
<Button android:id="@+id/top" android:nextFocusUp="@+id/bottom" ... />
<Button android:id="@+id/bottom" android:nextFocusDown="@+id/top" ... />
</LinearLayout>
我该怎么做?
THX。
答案 0 :(得分:0)
为此,您应该覆盖onKeyDown()
中的Activity
方法,在那里您可以使用方法View.getFocusedChild()
找到当前关注的视图,然后使用View.focusSearch(int)
搜索下一个视图,并{ {1}}获得焦点。
我希望你能把这些方法结合起来以获得好结果:)
答案 1 :(得分:0)
我找到了智能解决方案(例如下载):
bic=new BaseInputConnection(this.getWindow().getDecorView(),false);
KeyEvent event2 = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN,0, KeyEvent.META_SYM_ON, 0, 0, KeyEvent.FLAG_VIRTUAL_HARD_KEY);
bic.sendKeyEvent(event2);
多数民众赞成,一个内部的algortihm找到你所选方向的下一个元素