Missing accessibility label: provide either a view with an `android:labelFor` that references this view or provide an `android:hint`

Android Studio 2021.2.1 新定义 layout 文件的时候,如果 EditTextTextView 相邻定义,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Item Name" />

    <EditText
        android:id="@+id/edit_item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autofillHints="Item Name"
        android:inputType="text" />
</LinearLayout>

会在 EditText 收到警告信息:

“No label views point to this text field”

或者:

Missing accessibility label: provide either a view with an `android:labelFor` that references this view or provide an `android:hint`

这个警告的原因是:一般情况下,EditTextTextView 相邻的时候,TextView 一般用于提示用户应该输入何种内容,尤其是有多个 EditTextTextView 对的时候,可以通过指定 android:labelFor  来指出两者的对应关系。也可以通过给每个 EditText 增加一个 android:hit 方便用户理解。

解决方法如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:labelFor="@+id/edit_item_name"
        android:text="Item Name" />

    <EditText
        android:id="@+id/edit_item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autofillHints="Item Name"
        android:inputType="text" />
</LinearLayout>

或者:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Item Name" />

    <EditText
        android:id="@+id/edit_item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autofillHints="Item Name"
        android:hint="hit"
        android:inputType="text" />

</LinearLayout>

参考链接


Difference between android:id and android:labelFor?

发布者

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注