How to make TalkBack read TextView error message a

2019-04-11 10:18发布

问题:

I'm currently making my app accessible and I'm having problem with my EditTexts:

In every EditText, the user's input is being validated at some point (e.g. after pressing a button) and if the input is invalid, I show an error using editText.setError("message"). The problem is that if TalkBack is on, it will not automatically focus and read the error. Also, since I can't get the error's view, I can't ask TalkBack to focus it via sendAccessibilityEvent.

I would appreciate any ideas on how to solve this issue while still using editText.setError().

Edit 1 Added code for @Abhishek V solution:

public class BaseEditText extends EditText {

    ...
    ...

    @Override
    public void setError(CharSequence error) {
        super.setError(error);
        announceForAccessibility(error);
    }
}

回答1:

You can explicitly read out the error message through announceForAccessibility("mesage") function provided by View

editText.setError("message")
editText.announceForAccessibility("message");

Please note that this function was added in API level 16.

update 1: Set the error message to null when the text is changed in EditText to prevent reading error message again and again.

 editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                editText.setError(null);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });