简体   繁体   中英

Changing text spoken by talkback in Android

I am trying to change the text announced by TalkBack when an ImageView is focused through accessibility.

The Android documentation states that we should create an AccessibilityDelegate, and override onPopulateAccessibilityEvent (I am using the support library because I am also supporting GingerBread)

Thus, my code is the following:

public static void setImageDelegate(View view) {
    AccessibilityDelegateCompat delegate = new AccessibilityDelegateCompat() {
        @Override
        public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
            event.getText().add(event.getContentDescription() + ", image");
        }
    };
    ViewCompat.setAccessibilityDelegate(view, delegate);
}

When I call this function on my imageview, the delegate gets set, but the modified text is not being read. It simply reads the original content description. Am I doing something wrong or missing something about the accessibility functions?

Stepping through the code, it seems to be adding the correct text, but still, no change in spoken text.

Note: the above is a contrived example, content description could be used, but I'm trying to figure out why it doesn't work before I try it on custom views.

In ICS and above, TalkBack doesn't use the accessibility event text in most cases. Instead, it checks the text and content description of the AccessibilityNodeInfo exposed by the view. You would need to override onInitializeAccessibilityNodeInfo.

In most cases, though, you would just want to call View.setContentDescription.

In this particular case, you shouldn't set anything since TalkBack handles speaking control types and capabilities. We strongly advise developers against adding descriptions like "button" or "image."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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