Android可访问性:如何获得触摸的视图ID?

发布于 2025-02-04 09:38:54 字数 1534 浏览 4 评论 0原文

我已经陷入困境了几天。我想确定用户触摸的观点。这是我的可访问性服务XML:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
    android:accessibilityEventTypes="typeViewClicked"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagIncludeNotImportantViews|flagRetrieveInteractiveWindows|flagReportViewIds"
    android:canRetrieveWindowContent="true"
    android:description="@string/accessibility_service_description_3"
    xmlns:android="http://schemas.android.com/apk/res/android"/>

和服务:

@Override
protected void onServiceConnected() {
    super.onServiceConnected();

    AccessibilityServiceInfo configuration = new AccessibilityServiceInfo();
    configuration.eventTypes = AccessibilityEvent.TYPE_VIEW_CLICKED;
    configuration.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;

    configuration.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS
            | AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS
            | AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS;

    setServiceInfo(configuration);
}

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

    if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_CLICKED) {

        //Toast to show the id of the view that was clicked
        Toast.makeText(this, "" + event.getWindowId(), Toast.LENGTH_LONG).show();

    }

}

event.getWindowid()不起作用。我不确定它给出了谁的ID,但它为应用程序中的任何视图提供了相同的ID。我想要用户触摸的视图的ID。

I have been stuck on this for days. I want to identify the view that the user touches. Here is my accessibility service xml:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
    android:accessibilityEventTypes="typeViewClicked"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagIncludeNotImportantViews|flagRetrieveInteractiveWindows|flagReportViewIds"
    android:canRetrieveWindowContent="true"
    android:description="@string/accessibility_service_description_3"
    xmlns:android="http://schemas.android.com/apk/res/android"/>

And the service:

@Override
protected void onServiceConnected() {
    super.onServiceConnected();

    AccessibilityServiceInfo configuration = new AccessibilityServiceInfo();
    configuration.eventTypes = AccessibilityEvent.TYPE_VIEW_CLICKED;
    configuration.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;

    configuration.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS
            | AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS
            | AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS;

    setServiceInfo(configuration);
}

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

    if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_CLICKED) {

        //Toast to show the id of the view that was clicked
        Toast.makeText(this, "" + event.getWindowId(), Toast.LENGTH_LONG).show();

    }

}

event.getWindowId() is not working. I'm not sure whose ID it's giving but it gives the same ID for any view inside an app. I want the ID of the view that the user touched.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提赋 2025-02-11 09:38:54

您是否尝试过

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

获取源视图ID的完全合格的资源名称。

注意:此API的主要用法用于UI测试自动化,并且为了报告客户端必须访问的源查看ID在配置AccessibilityService时,设置AccessibilityServiceInfo#flag_report_view_ids标志。

Have you tried:

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

According to the docs:

Gets the fully qualified resource name of the source view's 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文