Android辅助功能在TextInputLayout中不起作用

问题描述

如果在TextInputEditText中没有键入任何文本,则Android Accessibility对讲程序将读取TextInputLayout中的提示

但是,如果在TextInputEditText中添加了文本,则Android Talkback将不会读取TextInputLayout中的提示

我需要辅助功能对讲以便始终阅读TextInputLayout中的提示

有人知道怎么做吗?

谢谢大家

        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:labelFor="@+id/editText"
            android:hint="username">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

        </com.google.android.material.textfield.TextInputLayout>

解决方法

将其覆盖为的唯一方法:

扩展:

public class CustomTextInputLayoutAccessibilityDelegate extends TextInputLayout.AccessibilityDelegate {...}

然后覆盖:

public void onInitializeAccessibilityNodeInfo(View host,AccessibilityNodeInfoCompat info) {

...
// And add the new logic in here:

        if (showingText) {
            if (hasHint) {
                info.setText(String.format("%s,%s",hintText,text));
            } else {
                info.setText(text);
            }
        } else if (hasHint) {
            info.setText(hintText);
        }


}

并将其设置为您的TextInputLayout为:

setTextInputAccessibilityDelegate(new CustomTextInputLayoutAccessibilityDelegate(this));