How to make TalkBack read TextView error message automatically?

情到浓时终转凉″ 提交于 2019-12-06 04:32:48

问题


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) {

            }
        });


来源:https://stackoverflow.com/questions/40705971/how-to-make-talkback-read-textview-error-message-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!