输入文本时如何更改EditText背景颜色?
How to change EditText background color when I enter text?
我的 EditText
有一些颜色,但是当我输入一些文本时它应该会改变。说它是灰色的,我开始进入它是黄色的。这个 EditText
也被聚焦,所以当这个 activity 开始时——我已经可以输入文本了。我是如何尝试的:
<style name="LoginEnterField">
<item name="android:textColorHint">@color/black</item>
<item name="android:textColor">@color/black</item>
<item name="android:background">@drawable/selector_edittext</item>
<item name="android:textSize">@dimen/general_text_size</item>
<item name="android:paddingLeft">@dimen/activity_inner_horizontal_margin</item>
<item name="android:paddingRight">@dimen/activity_inner_horizontal_margin</item>
</style>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/grey"/>
<corners android:radius="5dp" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/yellow"/>
<corners android:radius="5dp" />
</shape>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/roundbox_active_style"/>
<item android:drawable="@drawable/roundbox_inactive_style" />
</selector>
我认为关键在于这个选择器。我的 EditText 改变了颜色,但它总是黄色的。只有当我输入一些文字时,如何让它变成黄色? 我无法更改任何代码!这很重要。我无法仅添加或更改 xml 个文件。
编辑:仅从 xml 开始是不可能的,因此请更改代码。
使用TextWatcher
.
当一个类型的对象附加到一个 Editable 时,它的方法将在文本更改时被调用。
onTextChanged
(CharSequence s, int start, int before, int count) -
This method is called to notify you that, within s, the count
characters beginning at start have just replaced old text that had
length before.
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Your Code // call .setBackgroundColor method here
}
尝试使用 addTextChangedListener
addTextChangedListener(TextWatcher watcher)
Adds a TextWatcher to the list of those whose methods are called whenever this editText's text changes.
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (!editText.getText().toString().trim().isEmpty()) {
editText.setBackgroundColor(ContextCompat.getColor(Main2Activity.this, R.color.colorPrimary));
} else {
editText.setBackgroundColor(ContextCompat.getColor(Main2Activity.this, R.color.colorPrimaryDark));
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
使用 TextWatcher
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});
You can add your desired code in any of the method that suits you. For further reference visit:
https://developer.android.com/reference/android/text/TextWatcher.html
使用addTextChangedListener
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (!editText.getText().toString().trim().isEmpty()) {
editText.setBackgroundColor(Your New Color);
} else {
editText.setBackgroundColor(Your Old Color);
}
}
仅从 xml 是不可能的。无论如何,您需要更改代码。
我的 EditText
有一些颜色,但是当我输入一些文本时它应该会改变。说它是灰色的,我开始进入它是黄色的。这个 EditText
也被聚焦,所以当这个 activity 开始时——我已经可以输入文本了。我是如何尝试的:
<style name="LoginEnterField">
<item name="android:textColorHint">@color/black</item>
<item name="android:textColor">@color/black</item>
<item name="android:background">@drawable/selector_edittext</item>
<item name="android:textSize">@dimen/general_text_size</item>
<item name="android:paddingLeft">@dimen/activity_inner_horizontal_margin</item>
<item name="android:paddingRight">@dimen/activity_inner_horizontal_margin</item>
</style>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/grey"/>
<corners android:radius="5dp" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/yellow"/>
<corners android:radius="5dp" />
</shape>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/roundbox_active_style"/>
<item android:drawable="@drawable/roundbox_inactive_style" />
</selector>
我认为关键在于这个选择器。我的 EditText 改变了颜色,但它总是黄色的。只有当我输入一些文字时,如何让它变成黄色? 我无法更改任何代码!这很重要。我无法仅添加或更改 xml 个文件。
编辑:仅从 xml 开始是不可能的,因此请更改代码。
使用TextWatcher
.
当一个类型的对象附加到一个 Editable 时,它的方法将在文本更改时被调用。
onTextChanged
(CharSequence s, int start, int before, int count) - This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before.
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Your Code // call .setBackgroundColor method here
}
尝试使用 addTextChangedListener
addTextChangedListener(TextWatcher watcher)
Adds a TextWatcher to the list of those whose methods are called whenever this editText's text changes.
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (!editText.getText().toString().trim().isEmpty()) {
editText.setBackgroundColor(ContextCompat.getColor(Main2Activity.this, R.color.colorPrimary));
} else {
editText.setBackgroundColor(ContextCompat.getColor(Main2Activity.this, R.color.colorPrimaryDark));
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
使用 TextWatcher
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});
You can add your desired code in any of the method that suits you. For further reference visit:
https://developer.android.com/reference/android/text/TextWatcher.html
使用addTextChangedListener
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (!editText.getText().toString().trim().isEmpty()) {
editText.setBackgroundColor(Your New Color);
} else {
editText.setBackgroundColor(Your Old Color);
}
}
仅从 xml 是不可能的。无论如何,您需要更改代码。