![](/img/trans.png)
[英]How to change the background stroke color in TextInputLayout in android?
[英]Android: Change background color of Material TextInputLayout when active
我正在嘗試在 Material TextInputLayout 處於活動狀態時更改其背景顏色。 當我單擊 textinputedittext 時,它變得透明,我需要設置背景顏色。 我正在附加打印屏幕。
非常感謝
<com.google.android.material.textfield.TextInputLayout android:id="@+id/etPasswordLayout" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="24dp" android:layout_marginTop="24dp" android:layout_marginRight="24dp" android:hint="Enter your password" app:endIconTint="@null" app:endIconMode="password_toggle" android:textColorHint="@color/darkBlue" app:endIconDrawable="@drawable/ic_password_visibility" app:shapeAppearance="@style/Rounded"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/etPassword" style="@style/Text.NormalText" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/round_corner_toggle" android:inputType="textPassword" android:minHeight="60dp" android:text="" android:textColor="@color/darkBlue" /> </com.google.android.material.textfield.TextInputLayout>
textchangedlistener 的實現:
val layoutPassword = findViewById<TextInputLayout>(R.id.layout_Password)
val inputPassword = findViewById<TextInputEditText>(R.id.input_Password)
inputPassword.addTextChangedListener(object: TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
layoutPassword.setBackgroundColor(Color.parseColor("#D0DDFF"))
inputPassword.setBackgroundColor(Color.parseColor("#D0DDFF"))
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
layoutPassword.setBackgroundColor(Color.parseColor("#D0DDFF"))
inputPassword.setBackgroundColor(Color.parseColor("#D0DDFF"))
}
override fun afterTextChanged(s: Editable?) {
}
})
使用addTextChangedListener
field1 = (TextInputLayout)findViewById(R.id.field1);
field2 = (TextInputEditText)findViewById(R.id.field2);
field2.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// before text change
field1.setBackgroundColor() // set your color before active
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// after add text in TextInputEditText
field1.setBackgroundColor() // set your color after active
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.