如何从 EditText 文本更改更改 TextView 的背景?
How to change background of TextView From EditText Text Change?
我正在使用片段 class,我想在其中更改文本视图的颜色 当我的编辑文本上的文本发生更改时。在 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;
}
}
如有任何帮助,我们将不胜感激。
将 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){}
});
像这样使用 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))
您可以使用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"));
}
谢谢大家的回答@GuiLhe 我按照你的回答,我想改变TextView的背景颜色所以我写了
about_btn_Submit.setBackgroundColor(Color.parseColor("#9CCC65"));
而不是
textView.setTextColor(Color.parseColor("#9CCC65"));
再次感谢
我的解决方案是对@GuiihE 代码进行一些修改。
试试这个解决方案,它会根据您的要求解决:
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);
我正在使用片段 class,我想在其中更改文本视图的颜色 当我的编辑文本上的文本发生更改时。在 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;
}
}
如有任何帮助,我们将不胜感激。
将 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){}
});
像这样使用 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))
您可以使用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"));
}
谢谢大家的回答@GuiLhe 我按照你的回答,我想改变TextView的背景颜色所以我写了
about_btn_Submit.setBackgroundColor(Color.parseColor("#9CCC65"));
而不是
textView.setTextColor(Color.parseColor("#9CCC65"));
再次感谢
我的解决方案是对@GuiihE 代码进行一些修改。 试试这个解决方案,它会根据您的要求解决:
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);