2015-02-08 81 views
-2

我想验证我的android编辑文本框,以便当按钮被按下时,如果编辑文本框为空,则会出现一条消息,我也正在验证编辑文本框以确保输入的值是一个数字,如果文本框为空,则会显示错误消息,说明该值必须是数字,我认为这与我的if语句的结构有关,但我不确定什么。下面的代码是我已经尝试过的。如果语句没有按照正确的顺序排除语句

TextView Display; 
EditText Input; 

Display = (TextView) findViewById(R.id.tvOutput); 
Input = (EditText) findViewById(R.id.eName); 

try{ 
    String UserInput = Input.getText().toString(); 
    int number = Integer.parseInt(UserInput); 

    if(!UserInput.isEmpty()){ 
     if(number == (int) number){ 
      Display.setText(UserInput); 
     } 
    }else{ 
     Display.setText("Please enter a value into the text box"); 
    } 
}catch(Exception e){ 
    Display.setText("Please enter a number"); 
} 
+0

'如果(编号==(INT)号)'而'数字'已经是一个int? – timrau 2015-02-08 16:45:13

+0

即时解析通过用户输入存储在一个名为UserInput – Joshua 2015-02-08 16:47:20

+4

的字符串中'if(number ==(int)number)'是一个无用语句,它将始终为真。这就像比较'x == x'。 – initramfs 2015-02-08 16:50:00

回答

1

你的问题来自于Integer.parseInt()会抛出NumberFormatException为空字符串,在if语句运行之前将控制流发送到catch块。

将解析前的空检查重新排序为要修复的整数。

String UserInput = Input.getText().toString(); 

if(!UserInput.isEmpty()){ 
    try{ 
     int number = Integer.parseInt(UserInput); 

     Display.setText(UserInput); 
    }catch(NumberFormatException e){ 
     Display.setText("Please enter a number"); 
    } 
}else{ 
    Display.setText("Please enter a value into the text box"); 
} 

对于正确的Android的方式来过滤数值只考虑您的布局XML或或通过添加android:inputType=number欢迎使用属性到您的EditText元素:

EditText.setInputType(InputType.TYPE_CLASS_NUMBER); 

直接在java中。

+0

感谢您的帮助 – Joshua 2015-02-08 17:06:40

0

您需要验证输入的用户是否已经输入的数字,而不是字母数字值,你可以做如下:

String userInput = null; 
do { 
    Display.setText("Please enter a number"); 
    userInput = Input.getText().toString(); 
} while (!userInput.isEmpty() && !userInput.matches("[0-9]+")); 
Display.setText(userInput); 

上面做while循环,以确保用户输入数据及其有效输入。如果不是这样,那么继续获得用户输入。

最后一次,它的有效数字,只显示输入数字。

0

如果文本框为空,将显示错误消息说,值必须是数字

的问题是在这个片段:

try{ 
    String UserInput = Input.getText().toString(); 
    int number = Integer.parseInt(UserInput); 

    if(!UserInput.isEmpty()){ 

你试图将输入转换为整数,然后检查是否有可用的值。这意味着你可以尝试转换一个空字符串:""。这是行不通的,如果发生这种情况,Integer#parseInt将会抛出NumberFormatException。这将导致你到catch区块,它会打印“请输入一个数字”

为了解决这个问题,只是检查是否UserInput是空的,然后再尝试将输入转换成整数:

String userInput = input.getText().toString(); 
if (!userInput.isEmpty()) { 
    try{ 
     Integer.parseInt(userInput); // no need to store that result in a variable 
     display.setText(userInput); 
    } catch (Exception e) { 
     display.setText("Please enter a number"); 
    } 
} else { 
    display.setText("Please enter a value into the text box"); 
} 

另外,请阅读Java Naming Conventions,因为每一个变量应该以小写字母开头。我已经更改了我的代码示例中的变量名称。

+0

感谢您的帮助 – Joshua 2015-02-08 17:06:13

0

这2种辅助方法可以帮助你:

  1. 检查的EditText有文字。如果EditText上不具有文本设置错误消息

    public static boolean hasText(Context context, EditText editText) 
    { 
    boolean validated = true; 
    String text = editText.getText().toString().trim(); 
    if (text.length() == 0) 
    { 
        editText.setText(text); 
        validated = false; 
        Drawable d = context.getResources().getDrawable(R.drawable.alert_small); 
        d.setBounds(0, 0, 
          d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
        editText.setError(context.getString(R.string.required_field), d); 
    } 
    else 
    { 
        editText.setError(null); 
    } 
    return validated; 
    } 
    
  2. 检查文本数

    public static boolean isNumber(String str) 
    { 
    try 
    { 
        int d = Integer.parseInt(str); 
    } 
    catch (NumberFormatException nfe) 
    { 
        return false; 
    } 
    return true; 
    

    }