输入文本时如何更改EditText背景颜色? [英] How to change EditText background color when I enter text?

查看:136
本文介绍了输入文本时如何更改EditText背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 EditText 有一些颜色,但是当我输入一些文本时,它应该改变。说它是灰色,我开始输入,它是黄色。此 EditText 也是重点突出的,因此当此活动开始时-我已经可以输入文本了。我如何尝试:

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>

我认为重点在此选择器中。我的EditText更改颜色,但始终为黄色。仅当输入一些文字时,如何使其变成黄色? 我无法进行任何代码更改!这很重要。我不能仅添加或更改xml文件。

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.

编辑:仅从xml不可能做,所以要更改代码。

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

推荐答案

使用 TextWatcher

附加类型对象时更改为Editable时,将在更改文本时调用其方法。

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)-
调用此方法以通知您,在s内,从开始处开始的count
个字符刚刚替换以前具有
长度的旧文本。

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


    }

这篇关于输入文本时如何更改EditText背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆