How to change background of TextView From EditText

2019-09-20 08:38发布

问题:

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.

回答1:

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



回答2:

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



回答3:

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


回答4:

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


回答5:

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