-1

mail_xml is this:

EditText android:inputType="numberDecimal|numberSigned`

onTextChanged implemented on this and other edittexts and calls relative methods.

//and one method example:
if (editTextNumber.getText().toString().equals("") ||                
    editTextNumber.getText().toString().equals("-") ||  
    editTextNumber.getText().toString().equals(".")) {
//say its bad or reset interface so the user knows;

else {
//do stuff;
}

Should I create a class to do the validation and instantiate it, then use an if/else statement returning a boolean?

It's difficult because a - is valid as the user types, so is a .. But a . and - crashes. I'm thinking a class would be best?

I don't remember a function in java to do this, unless the api has been updated and I haven't seen it yet. I've created the classes i need and can instantiate them call their getter methods no problem. just stuck on the basic validation.

4

1 回答 1

0

您可以简单地将演员表放在一个块中 try/catch... 这样如果插入的文本不是数字,则会触发异常...例如:

if(editTextNumber.getText()!=null && !editTextNumber.getText().toString().equals("")){
try{
    // int value
    Integer.parseInt(editTextNumber.getText().toString());
    // double value
    Double.parseDouble(editTextNumber.getText().toString());
}catch(Exception e){
    //prints the exception that you got
    //if number format exception then your input is not a number
    e.printStackTrace();
}
}

希望它有所帮助...

于 2013-05-22T16:24:56.840 回答