1

我的辅助功能服务将通过可聚焦节点。我正在尝试为此使用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

所以问题是我如何才能通过布局中的可聚焦节点?

4

2 回答 2

3

这是一个普遍的误解。您正在搜索的是可以获取输入焦点的对象(想想 EditText 控件)。这与可访问性焦点根本不同。您的布局中没有输入可聚焦控件。

您想要的功能是:

someAccessibilityNodeInfo.getTraversalAfter();

someAccessibilityNodeInfo.getTraversalBefore();

没有上/下等价物。尽管您可以随心所欲地计算它。您可以将可访问性可聚焦节点的整个层次结构存储在一个数组中,并根据 x 和 y 位置计算上/下。

于 2015-10-11T23:02:25.723 回答
1

确切地!视图容器被返回,因为它们存在于树中。

你需要的是叶子节点。

此代码将为您提供可聚焦的节点(叶节点)列表:

ArrayList<AccessibilityNodeInfo> inputViewsList = new ArrayList<AccessibilityNodeInfo>();
AccessibilityNodeInfo rootNode = getRootInActiveWindow();
refreshChildViewsList(rootNode);
for(AccessibilityNodeInfo mNode : inputViewsList){
    if(mNode.isFocusable()){
        //do whatever you want with the node...
    }
}

refreshChildViewsList方法:

private void refreshChildViews(AccessibilityNodeInfo rootNode){
    int childCount = rootNode.getChildCount();
    for(int i=0; i<childCount ; i++){
        AccessibilityNodeInfo tmpNode = rootNode.getChildAt(i);
        int subChildCount = tmpNode.getChildCount();
        if(subChildCount==0){
            inputViewsList.add(tmpNode);
            return; 
        } else {
            refreshChildViews(tmpNode);
        } 
    } 
} 

让我知道此代码是否有任何问题!

于 2015-10-20T13:00:23.137 回答