简体   繁体   English

如何使用 android 无障碍服务来检测用户触摸了哪个视图?

[英]How to use an android accessibility service to detect which view the user touched?

There's something that android TalkBack does that I want to do too. android TalkBack 有一些我想做的事情。 Specifically it's to identify the view that the user touched.具体来说,它是识别用户触摸的视图。 I made this so far:到目前为止我做了这个:

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_CLICKED) {

        //This only shows the package of the activity that the view is in, I want to identify the view itself
        Toast.makeText(this, "" + event.getSource().getPackageName(), Toast.LENGTH_LONG).show();
    }
}

You can get the view's id, but I think you can only access the AccessibilityNodeInfo associated with the view - I don't think you have access to the view itself.您可以获得视图的 id,但我认为您只能访问与视图关联的AccessibilityNodeInfo - 我认为您无权访问视图本身。 You can check the docs for more information on this.您可以查看文档以获取更多信息。

    // kotlin code
    event.source.viewIdResourceName
    // or
    findFocusedViewInfo().viewIdResourceName

According to the docs :根据文档

Gets the fully qualified resource name of the source view's id.获取源视图 id 的完全限定资源名称。

Note: The primary usage of this API is for UI test automation and in order to report the source view id of an AccessibilityNodeInfo the client has to set the AccessibilityServiceInfo#FLAG_REPORT_VIEW_IDS flag when configuring the AccessibilityService.注意:此 API 的主要用途是 UI 测试自动化,为了报告 AccessibilityNodeInfo 的源视图 ID,客户端必须在配置 AccessibilityService 时设置 AccessibilityServiceInfo#FLAG_REPORT_VIEW_IDS 标志。

the question is not clear, but maybe this will help you:-问题尚不清楚,但也许这会对您有所帮助:-

@Override 
public void onAccessibilityEvent(AccessibilityEvent event) {
    AccessibilityNodeInfo source = event.getSource(); 

    if (source == null) {
        return; 
    } 
    List<AccessibilityNodeInfo> findAccessibilityNodeInfosByViewId = source.findAccessibilityNodeInfosByViewId("YOUR PACKAGE NAME:id/RESOURCE ID FROM WHERE YOU WANT DATA"); 

    if (findAccessibilityNodeInfosByViewId.size() > 0) {
        AccessibilityNodeInfo parent = (AccessibilityNodeInfo) findAccessibilityNodeInfosByViewId.get(0);
        // You can also traverse the list if required data is deep in view hierarchy. 
        String requiredText = parent.getText().toString();
        Log.i("Required Text", requiredText);
    }
}

also read this tutorial还阅读本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM