所以我有一个EditText字段,我想用它来检查年龄是否在我的限制范围之内。
if (Integer.parseInt(age.getText().toString()) < 18 && Integer.parseInt(age.getText().toString()) >= 0)但是,我也想简单地检查一下字段是否为空,这是我所使用的。
else if (age.getText().toString().isEmpty())
不幸的是这个不能工作,我认为它和第一个或其他东西有冲突,因为我只尝试了两个条件中的一个,它是有效的.我还尝试将检查isEmpty()的方法存储在字符串变量中,也尝试存储在int变量中进行年龄比较,但它仍然不起作用。
提前谢谢。
发布于 2021-06-02 04:53:40
代码应该是:
if (age.getText().toString().trim().isEmpty()){
//The EditText field is empty
}else{
try{
if (Integer.parseInt(age.getText().toString().trim()) < 18 && Integer.parseInt(age.getText().toString().trim()) >= 0){
//Your code here
}else{
//Input value is over 18 or under 0
}
}catch(Exception ex){
//There was an error parsing the input (if your user writes letters, parseInt fails)
}
}https://stackoverflow.com/questions/67795773
复制相似问题