[英]How can change icon color of EditText
我想以編程方式更改 EditText 圖標的顏色。 檢查我的截圖。 當輸入有錯誤時,我希望圖標變為紅色。https://i.stack.imgur.com/Ih9tA.jpg。
我的 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>
我的活動代碼是
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();
}
我的矢量代碼是
<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>
您將需要使用 TextInputEditText 而不是普通的 EditText
這是一個例子
<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.