How to change background of TextView From EditText

2019-09-20 08:51发布

I am using Fragment class in which I want to change the color of Text View When text is changed on my edit Text. What are the possible ways to change the background color of TextView When Text is Changed in EditText . My code looks like this :

public class Fragment_AboutUs extends android.app.Fragment {
    TextView about_btn_call_customer;
    TextView about_btn_Submit;
    EditText about_feedback;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.layout_fragment_about_us, container, false);
        //reference section
        about_btn_call_customer= (TextView) rootView.findViewById(R.id.about_btn_call_customer);
        about_btn_Submit=(TextView)rootView.findViewById(R.id.about_btn_Submit);
        about_feedback=(EditText)rootView.findViewById(R.id.about_feedback);
        return rootView;
    }
}

Any kind of help would be appreciated.

5条回答
【Aperson】
2楼-- · 2019-09-20 09:09

Add TextChangedListener to your EditText as follows:

about_feedback.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {
        // do what ever you want to TextView
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
}); 
查看更多
We Are One
3楼-- · 2019-09-20 09:21

Use a TextWatcher like this:

TextWatcher textWatcher = 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) {
        textView.setTextColor(Color.parseColor("#..."));
    }
};

editText.addTextChangedListener(textWatcher);

Note: You can also use textView.setTextColor(getResources().getColor(R.color.mycolor))

查看更多
Deceive 欺骗
4楼-- · 2019-09-20 09:28

My solution is a little modification of @GuiihE codes. Try this solution,it would work out as per your requirements:

TextWatcher textWatcher = 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) {
    about_btn_Submit.setBackgroundColor(Color.parseColor("#9CCC65"));
}

};

editText.addTextChangedListener(textWatcher);

查看更多
劫难
5楼-- · 2019-09-20 09:29

Thank you Guys for the answer @GuiLhe i Followed your answer,I wanted to change the background color of TextView so I wrote

about_btn_Submit.setBackgroundColor(Color.parseColor("#9CCC65"));

instead of

textView.setTextColor(Color.parseColor("#9CCC65"));

Thank you again

查看更多
女痞
6楼-- · 2019-09-20 09:30

You can use TextWatcher

for eg:

EditText about_feedback= (EditText)findViewById(R.id.medittext);
about_feedback.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        doSomething();



    } 

});

and you can use

about_btn_Submit.setTextColor(Color.parseColor("#FFFFFF"));

to change the color of the TextView

So,

in your code, do this in onTextChanged(), ie

public void onTextChanged(CharSequence s, int start, int before, int count) {
     about_btn_Submit.setTextColor(Color.parseColor("#FFFFFF"));
} 
查看更多
登录 后发表回答