我正在尝试根据 EditText 中文本的长度来编辑 Button 的背景。如果文本的长度为 0,那么背景应该是半透明的,否则它应该是不透明的。但是,我无法动态更新按钮的背景。如果我点击屏幕上的其他地方,按钮的背景就会更新,但不是实时的。
这是我正在使用的代码。
emailEditText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(s.length()!=0){
submitEmail.getBackground().setAlpha(255);
}
else{
submitEmail.getBackground().setAlpha(45);
}
}
});
我们将不胜感激任何帮助。
最佳答案
请在设置值后调用
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(s.length()!=0){
submitEmail.getBackground().setAlpha(255);
}
else{
submitEmail.getBackground().setAlpha(45);
}
submitEmail.invalidate();
}
关于android - 无法在 addTextChangedListener() 中更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15426095/