我的辅助功能服务将通过可聚焦节点。我正在尝试为此使用focusSearch功能,但这并没有返回任何节点。但是布局包含可聚焦的项目。这是布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:text="Button" />
<TextView
android:id="@+id/text"
android:layout_width="122dp"
android:layout_height="fill_parent"
android:text="Hello World2" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:focusable="true"
android:text="Button2" />
</LinearLayout>
所以我试图找到第一个按钮:
AccessibilityNodeInfo root = getRootInActiveWindow();
AccessibilityNodeInfo node1 = null;
node1 = root.findAccessibilityNodeInfosByViewId("my.app:id/button1").get(0);
//returns button view
node1 = root.focusSearch(View.FOCUS_DOWN); //returns null
node1 = root.focusSearch(View.FOCUS_LEFT); //returns null
node1 = root.focusSearch(View.FOCUS_RIGHT); //returns null
node1 = root.focusSearch(View.FOCUS_FORWARD); //returns null
但是使用findFocus它会返回框架布局
node1 = root.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY);
//returns node of the main framelayout
然后我使用这个节点进行下一次搜索,但没有再次找到。
node1 = node1.focusSearch(View.FOCUS_FORWARD); //returns null
当我打电话findfocus
而不是focusSearch
我得到相同的节点时
node1 = node1.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY);
//returns the same node
所以问题是我如何才能通过布局中的可聚焦节点?