How to change EditText background color when I ent

2019-09-21 05:41发布

问题:

My EditText has some color, but when I enter some text it should change. Say it's grey, I started to enter and it's yellow. This EditText is also focused, so when this activity starts -- I already can enter the text. How I tried:

<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>

I think the point is in this selector. My EditText changes color, but it always yellow. How to make it became yellow only if I enter some text? I can't make any code changes! It's very important. I can't add or change only xml files.

EDIT: It's impossible to do from xml only, so make code changes.

回答1:

Use TextWatcher.

When an object of a type is attached to an Editable, its methods will be called when the text is changed.

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


    }


回答2:

Try this use 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) {

            }
        });


回答3:

Use 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



回答4:

Use 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);
                }
            }


回答5:

It's impossible to do from xml only. You anyway need to make a code changes.