【问题标题】:How to change background of TextView From EditText Text Change?如何从 EditText 文本更改更改 TextView 的背景?
【发布时间】:2015-05-21 11:09:30
【问题描述】:

我正在使用 Fragment 类,当我在编辑文本上更改文本时,我想在其中更改文本视图的颜色。在 EditText 中更改文本时更改 TextView 的背景颜色的可能方法是什么。我的代码如下所示:

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

我们将不胜感激。

【问题讨论】:

    标签: android colors background android-edittext textview


    【解决方案1】:

    我的解决方案是对@GuihE 代码稍作修改。 试试这个解决方案,它会根据您的要求工作:

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

    【讨论】:

    • 我很高兴@SantoshBhandary
    【解决方案2】:

    像这样使用 TextWatcher:

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

    注意:您也可以使用textView.setTextColor(getResources().getColor(R.color.mycolor))

    【讨论】:

      【解决方案3】:

      TextChangedListener 添加到您的EditText,如下所示:

      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】:

        您可以使用TextWatcher

        例如:

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

        你可以使用

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

        改变TextView的颜色

        所以,

        在您的代码中,在onTextChanged() 中执行此操作,即

        public void onTextChanged(CharSequence s, int start, int before, int count) {
             about_btn_Submit.setTextColor(Color.parseColor("#FFFFFF"));
        } 
        

        【讨论】:

          【解决方案5】:

          谢谢大家的回答@GuiLhe 我关注了你的回答,我想改变TextView的背景颜色所以我写了

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

          而不是

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

          再次感谢您

          【讨论】:

            猜你喜欢
            • 2012-05-28
            • 1970-01-01
            • 1970-01-01
            • 2012-04-19
            • 1970-01-01
            • 1970-01-01
            • 2014-11-08
            • 1970-01-01
            • 2015-12-17
            相关资源
            最近更新 更多