Изначально вы можете использовать селектор для атрибута EditText
backgroundTint
, который выбирает правильный цвет, когда он находится в фокусе или нет
селектор. 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
просмотров