首页 > 解决方案 > setEnabled(false) 时显示 TextInputEditText 错误

问题描述

我目前有一个 TextInput,一旦选择了微调器位置,它就会显示所需的计数作为错误。该部分有效,但我还希望在仍需要选择位置时显示错误。当我使 TextInput 不可编辑时,错误不会显示并且我看不到或单击它。我尝试使用 setFocusable(),但这允许编辑文本。

    if (curr_position != 0) {
        quantity_field.setEnabled(true);
        quantity_field.requestFocus();
        quantity_field.setError(quantities[parent.getSelectedItemPosition()]);
    } else {
        quantity_field.setError("Select location first.");
        quantity_field.setEnabled(false);
    }

本质上,我想禁用除错误之外的 TextInput 的所有部分。

标签: javaandroidandroid-textinputedittext

解决方案


您是否尝试过将您app:errorEnabled="true"的属性设置为TextInputLayout, 并从您的 xml 文件中将 clickable、cursorVisible、focusable、focusableInTouchMode 设置为 false?

<android.support.design.widget.TextInputLayout
    android:id="@+id/quantity_field"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorEnabled="true">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/quantity_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="false" 
        android:cursorVisible="false" 
        android:focusable="false" 
        android:focusableInTouchMode="false"
        android:hint="Location:" />

</android.support.design.widget.TextInputLayout>

代码:

if (curr_position != 0)
    quantity_field.setError("Select location first.");
else
    quantity_field.setError(null); // hides the error and reset the tint

推荐阅读