如果它充满输入,更改编辑文本的下划线颜色?
Change underline color of edittext if it is filled with input?
如果编辑文本被输入填充,更改下划线颜色?
状态:
- 不专心:color/1
- 专注:color/2
- 未聚焦(输入后):color/2
我试过使用 :
edtText.doAfterTextChanged {
}
但是,我在 doAfterTextChanged
中找不到属性 "edtText.backgroundTint"
您可以使用 TextWatcher
和 setBackgroundTintList
来更改线条颜色,
et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s != null && !(s.toString().isEmpty())) {
//Color when text exists
ColorStateList colorStateList = ColorStateList.valueOf(getResources().getColor(R.color.colorPrimaryDark));
ViewCompat.setBackgroundTintList(et, colorStateList);
} else {
//Default color - color when text does not exist
ColorStateList colorStateList = ColorStateList.valueOf(getResources().getColor(R.color.colorAccent));
ViewCompat.setBackgroundTintList(et, colorStateList);
}
}
@Override
public void afterTextChanged(Editable s) {}
});
请参考这个。
您最初可以将 selector 用于 EditText
backgroundTint
属性,它会在聚焦与否时选择正确的颜色
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/focused" android:state_focused="true" />
<item android:color="@color/unfocused" android:state_focused="false" />
</selector>
您的布局
<EditText
android:id="@+id/edit_text1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:backgroundTint="@drawable/selector" />
并且在更改文本时使用 TextUtils.isEmpty()
检查它是否为空,并相应地更改颜色:
EditText editText1 = findViewById(R.id.edit_text1);
editText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (!TextUtils.isEmpty(editText1.getText().toString()))
editText1.setBackgroundTintList(ContextCompat.getColorStateList(getApplicationContext(), R.color.focused));
else
editText1.setBackgroundTintList(ContextCompat.getColorStateList(getApplicationContext(), R.color.unfocused));
}
});
对所有其他 EditText
视图重复相同的操作
如果编辑文本被输入填充,更改下划线颜色?
状态:
- 不专心:color/1
- 专注:color/2
- 未聚焦(输入后):color/2
我试过使用 :
edtText.doAfterTextChanged {
}
但是,我在 doAfterTextChanged
中找不到属性 "edtText.backgroundTint"您可以使用 TextWatcher
和 setBackgroundTintList
来更改线条颜色,
et.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s != null && !(s.toString().isEmpty())) {
//Color when text exists
ColorStateList colorStateList = ColorStateList.valueOf(getResources().getColor(R.color.colorPrimaryDark));
ViewCompat.setBackgroundTintList(et, colorStateList);
} else {
//Default color - color when text does not exist
ColorStateList colorStateList = ColorStateList.valueOf(getResources().getColor(R.color.colorAccent));
ViewCompat.setBackgroundTintList(et, colorStateList);
}
}
@Override
public void afterTextChanged(Editable s) {}
});
请参考这个
您最初可以将 selector 用于 EditText
backgroundTint
属性,它会在聚焦与否时选择正确的颜色
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/focused" android:state_focused="true" />
<item android:color="@color/unfocused" android:state_focused="false" />
</selector>
您的布局
<EditText
android:id="@+id/edit_text1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:backgroundTint="@drawable/selector" />
并且在更改文本时使用 TextUtils.isEmpty()
检查它是否为空,并相应地更改颜色:
EditText editText1 = findViewById(R.id.edit_text1);
editText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (!TextUtils.isEmpty(editText1.getText().toString()))
editText1.setBackgroundTintList(ContextCompat.getColorStateList(getApplicationContext(), R.color.focused));
else
editText1.setBackgroundTintList(ContextCompat.getColorStateList(getApplicationContext(), R.color.unfocused));
}
});
对所有其他 EditText
视图重复相同的操作