[英]How to change background color of editText?
I need to change background color of EditText
if it is empty. 我需要更改EditText
背景颜色,如果它是空的。 Below is my code but doesn't seem to be working. 下面是我的代码,但似乎没有工作。
public void onClick(View v) {
// TODO Auto-generated method stub
Change();
}
public void Change() {
if(("").equals(name)) {
name.setBackgroundColor(Color.RED);
return;
}
}
Assuming 'name' is the EditText in question, your if statement should read something like this: 假设'name'是有问题的EditText,你的if语句应该是这样的:
if(("").equals(name.getText().toString()))
Performing an Object.equals(Object) between a String and an EditText (at least currently!) will not return true. 在String和EditText之间执行Object.equals(Object)(至少当前!)将不会返回true。
The easiest way is to check the length: 最简单的方法是检查长度:
if (name.length() == 0) {
name.setBackgroundColor(Color.RED);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.