【发布时间】:2022-01-24 08:50:43
【问题描述】:
我想检查 EditText 的值是否大于 5,我希望 edittext 的背景变为红色,但它会在我删除文本时停止。请帮忙。
vibration.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
vibration.setBackgroundColor(Color.YELLOW);
}
@Override
public void afterTextChanged(Editable s) {
float numV = Float.parseFloat(vibration.getText().toString());
if(numV > 5){
vibration.setBackgroundColor(Color.RED);
}else{
return;
}
}
});
【问题讨论】:
-
空文本不是数字给出空引号“”,启用 parseFloat 异常即将到来。
-
所以我应该处理这个异常?
-
可以检查字符串是否为空然后解析成float,否则返回
标签: java android-studio