【问题标题】:GetText compareGetText 比较
【发布时间】:2021-06-01 20:44:52
【问题描述】:

所以我有一个 EditText 字段,我想检查年龄是否高于和低于我的限制。

if (Integer.parseInt(age.getText().toString()) < 18 && Integer.parseInt(age.getText().toString()) >= 0)

而且,我想简单地检查一下我使用的字段是否为空。

else if (age.getText().toString().isEmpty())

不幸的是,这个不起作用,我认为它与第一个或其他东西发生冲突,因为我只尝试了两者的一个条件,它起作用了.. 我还尝试将检查 isEmpty() 的方法存储在 String 变量中,以及 int one 中,以进行年龄比较,但它仍然无法正常工作。

提前致谢。

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

    代码应该是:

    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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多