android - 从 Android Assist API 检索所有文本内容

标签 android ellipsis

我有一个方法可以通过 Assist API 遍历所有 ViewNode:

@Override
public void onHandleAssist(Bundle data, AssistStructure structure, AssistContent content) {
    super.onHandleAssist(data, structure, content);
    mContent = "";
    mDescription = "";
    String structuredData = content.getStructuredData();
    if (!TextUtils.isEmpty(structuredData)) {
        structuredData = structuredData.replace("\\", "");
        mContent += structuredData + " ";
    }
    Bundle extra = content.getExtras();
    ClipData clipData = content.getClipData();
    Intent intent = content.getIntent();
    Bundle intentExtra = intent.getExtras();
    Uri uri = content.getWebUri();
    if (uri != null) {
        mContent += uri.toString() + " ";
    }
    ComponentName componentName = structure.getActivityComponent();
    int nodeCount = structure.getWindowNodeCount();
    for (int i = 0; i < nodeCount; i++) {
        AssistStructure.WindowNode windowNode = structure.getWindowNodeAt(i);
        getAllContentText(windowNode.getRootViewNode(), 5);
    }
    initView();
}

private void getAllContentText(AssistStructure.ViewNode node, int levelMax) {

    if (node != null) {
        int nodeCount = node.getChildCount();
        if (!TextUtils.isEmpty(node.getText())) {
            mContent += node.getText().toString() + " ";
        }

        if (!TextUtils.isEmpty(node.getContentDescription())) {
            mDescription += node.getContentDescription() + " ";
        }
        for (int i = 0; i < nodeCount; i++) {
            AssistStructure.ViewNode childNode = node.getChildAt(i);
            getAllContentText(childNode, levelMax);
        }
    }
}

但是如果一个文本被省略了,我怎样才能得到所有的文本呢?或者如果文本是链接文本,我可以获取与之关联的 url 吗?

最佳答案

我相信您不能直接引用应用程序的 View ,您从 AssistStructure 获得的只是 AssistStructure.ViewNode。 View 节点的 getText() 方法将可见文本返回给用户。

也许如果您找到某种方法通过您的 VoiceInteractionSession getContext() 方法引用 Window 对象。您可以将上下文转换为 ActivityWindow,然后调用 getDecorView(),然后遍历 subview 以从 TextView 获取完整文本。

关于android - 从 Android Assist API 检索所有文本内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37295155/

相关文章:

uwp - 在 UWP (Windows 10) 中隐藏 CommandBar 中的省略号按钮(更多按钮)

c - 如何用 C 语言编写带有可变参数的函数

android - Jetpack Compose 填充行中的剩余空间

android - 为什么 FirebaseDatabase Client Offline 有两种方法?

R 省略号 - 传递给其他方法或从其他方法传递的进一步参数

python - 调用 Python doctest 时如何启用省略号?

c++ - 计算 Blob 的方向

python - 在您的应用程序目录中找到构建失败 : No main. py(o)

java - 我应该使用 Handler 类还是同步函数进行线程间通信

android - 更新到 AS 3.0 Canary 6 后 transformClassesWithDesugar 构建错误