禁用文本输入布局的错误文本提示或更改edittext下划线颜色

时间:2018-03-23 17:05:40

标签: android android-edittext material-design androiddesignsupport

是否可以在文本输入布局上设置错误时禁用提示。我尝试传入null和一个空字符串,但在那时它不会使行变为红色。

我也传入一个带有空格的字符串,但是如果我这样做,那么提示仍占据编辑文本下划线的空间。

我尝试将下划线颜色更改为红色,但似乎需要做很多工作。我目前正在使用API​​ 19.

那么可以禁用提示吗?

由于

2 个答案:

答案 0 :(得分:0)

所以我不认为可以禁用错误的提示(我可能是非常错误的)。处理此问题的最佳方法是使用setSupportBackgroundTintList或setBackgroundTintList更改editText中行的颜色。

一些关键的事情。

1)如果您使用的是sdkVersion 23或更高版本,则可以使用常规的editText和setBackgroundTintList方法。

2)如果你在23-19之间,我认为你也可以在较低的sdkVersions上使用它。您必须在布局中使用Appcompat EditText。

3)使用setSupportBackgroundTintList将显示为错误,但这是因为支持库存在错误。您可以在您正在使用它的方法/位置中抑制lint错误。

以下是一些代码段。我希望这可以帮助那些只想改变editText中线条颜色的人。

我的布局中的AppCompatEditText。

<android.support.design.widget.TextInputLayout
        android:id="@+id/login_user_password_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:hint="@string/login_hint_password"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/login_user_email_layout"
        app:layout_constraintBottom_toTopOf="@id/error_text"
        app:layout_constraintRight_toRightOf="parent">

        <android.support.v7.widget.AppCompatEditText
            android:id="@+id/login_user_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:imeOptions="actionDone"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"/>

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

方法我在下面设置下划线颜色。

@SuppressLint("RestrictedApi")
private void setEditTextFields(){
    if(errorLoginIn){
        errorLoginIn = false;
        binding.errorText.setVisibility(View.VISIBLE);
        binding.loginUserEmail.setSupportBackgroundTintList(getResources().getColorStateList(R.color.colorError));
        binding.loginUserPassword.setSupportBackgroundTintList(getResources().getColorStateList(R.color.colorError));
    }else{
        binding.errorText.setVisibility(View.INVISIBLE);
        binding.loginUserEmail.setSupportBackgroundTintList(getResources().getColorStateList(R.color.primaryColor));
        binding.loginUserPassword.setSupportBackgroundTintList(getResources().getColorStateList(R.color.primaryColor));
    }
}

绑定是布局,因为我使用数据绑定。

希望这有助于某人。

答案 1 :(得分:0)

是的,可以仅禁用错误文本并显示带下划线的红线。尽管@ huey77回答可能有所帮助,但它很复杂,需要手动控制状态

我的解决方案很简单,只需将errorTextAppearance与0dp一起使用:

 <style name="TextError" parent="TextAppearance.AppCompat">
    <item name="android:textSize">0dp</item>
    <item name="android:textColor">?colorError</item>
</style>

<style name="TextInputLayout" parent="Widget.Design.TextInputLayout">
    <item name="errorTextAppearance">@style/TextError</item>
</style>

您的应用主题中:

<item name="textInputStyle">@style/TextInputLayout</item>