2015-05-21 2 views
0

Я использую класс Fragment, в котором я хочу изменить цвет TextView Когда EditText пуст после его изменения. Я могу изменить цвет TextView при изменении текста, но когда текст будет чистым, цвет фона TextView не изменится на предыдущий. Каковы возможные способы изменения цвета фона TextView Когда EditText пуст после изменения текста. Мой код выглядит следующим образом:Как изменить цвет фона TextView, если EditText пуст в android?

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

}

Спасибо заранее ... любая помощь была бы оценена ...

ответ

1

чек countonTextChanged и выберите цвет:

@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

+1

я попробовал свое решение, он работал как шарм, спасибо –

+0

вы можете – Blackbelt

0

Попробуйте это,

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) {} 
    }; 
Смежные вопросы