[英]How can change icon color of EditText
I want change color of EditText Icon programmatically.我想以编程方式更改 EditText 图标的颜色。 Check my screenshot.
检查我的截图。 I want icon take red color when input has error.https://i.stack.imgur.com/Ih9tA.jpg.
当输入有错误时,我希望图标变为红色。https://i.stack.imgur.com/Ih9tA.jpg。
My xml code is我的 xml 代码是
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/layout_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:hint="@string/register_hint_name"
android:labelFor="@+id/username_input_text"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<EditText
android:id="@+id/username_input_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:drawableStart="@drawable/ic_user"
android:drawableLeft="@drawable/ic_user"
android:drawablePadding="15dp"
android:autofillHints="false"/>
</com.google.android.material.textfield.TextInputLayout>
My Activity code is我的活动代码是
if (username_text.isEmpty() || email_text.isEmpty() || password_text.isEmpty() || con_password_text.isEmpty()) {
layout_username.setError("The field Username is required!");
username_input_text.requestFocus();
}
My vektor code is我的矢量代码是
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:name="user_icon"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,5c1.66,0 3,1.34 3,3s-1.34,3 -3,3 -3,-1.34 -3,-3 1.34,-3 3,-3zM12,19.2c-2.5,0 -4.71,-1.28 -6,-3.22 0.03,-1.99 4,-3.08 6,-3.08 1.99,0 5.97,1.09 6,3.08 -1.29,1.94 -3.5,3.22 -6,3.22z"/>
</vector>
you will need to use TextInputEditText instead of the normal EditText您将需要使用 TextInputEditText 而不是普通的 EditText
here's an example这是一个例子
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:boxStrokeErrorColor="@android:color/red"
app:errorIconDrawable="@drawable/icon"
app:errorTextColor="@android:color/white">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.