如果 android 中 EditText 为空,如何更改 TextView 的背景颜色?

how to change the background color of TextView if EditText is empty in android?

我正在使用 Fragment class,其中我想更改 TextView 的颜色 当 EditText 文本更改后为空时。我可以在文本更改时更改 TextView 的颜色,但是当文本清晰时,TextView 背景颜色不会更改为之前的颜色。更改文本后 TextView 的背景颜色有哪些可能的方法 当 EditText 为空时。我的代码如下所示:

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


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

                String textinEdit=  about_feedback.getText().toString().trim();
                if (textinEdit != null) {
                    about_btn_Submit.setBackgroundColor(getResources().getColor(R.color.light_green));

                }else if (textinEdit ==""){
                    about_feedback.setBackgroundColor(getResources().getColor(R.color.grayColor));
                }

            }
        };

    about_feedback.addTextChangedListener(textWatcher);






    return rootView;
}

}

提前致谢...任何形式的帮助将不胜感激...

试试这个,

   TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() == 0) {
                // for empty text color
                about_btn_Submit.setBackgroundColor(getResources().getColor(R.color.grayColor));
            } else { 
                // for non empty field color
                about_btn_Submit.setBackgroundColor("change your color");
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override
        public void afterTextChanged(Editable s) {}
    };

选中count onTextChanged并选择颜色:

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
   int color = count == 0 ? R.color.light_green : R.color.grayColor;
   about_btn_Submit.setBackgroundColor(getResources().getColor(color));
 }

你可以对另一个人做同样的事情TextView