在提交时验证字段

时间:2014-02-28 10:49:34

标签: android validation android-edittext

我的程序中有2个Edittext和一个按钮。我想检查edittexts的内容是否与模式匹配,它确实执行了一些计算。所以点击按钮,在我的Java文件中创建了相应的方法,但我在日志猫中不断收到错误。这是我的onClick方法。

public void multiply(){
    final EditText start  = (EditText) findViewById(R.id.editText_InputStart);
    final EditText stop = (EditText) findViewById(R.id.editText_InputStop);

    if(start.getText().toString().length() == 0){
        start.setError("Enter at least a Number");
        if(!start.getText().toString().matches("[0-9]")){
            start.setError("Enter a Number");
            if (stop.getText().toString().length() == 0){
                stop.setError("Enter at least a Number");
                if (!stop.getText().toString().matches("[0-9]")){
                    stop.setError("Enter a Number");
                }
            }
        }
    } else{
        calculate(start.getText().toString(), stop.getText().toString());
    }               
}

以下是日志cat错误。

02-28 06:38:52.308: E/AndroidRuntime(1125): FATAL EXCEPTION: main
02-28 06:38:52.308: E/AndroidRuntime(1125): Process: com.wecanIT.multiplicationtables,     PID: 1125
02-28 06:38:52.308: E/AndroidRuntime(1125): java.lang.IllegalStateException: Could not  find a method multiply(View) in the activity class  com.wecanIT.multiplicationtables.MultiplyMainActivity for onClick handler on view class  android.widget.Button with id 'process'
02-28 06:38:52.308: E/AndroidRuntime(1125): at android.view.View$1.onClick(View.java:3810)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at android.view.View.performClick(View.java:4438)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at android.view.View$PerformClick.run(View.java:18422)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at android.os.Handler.handleCallback(Handler.java:733)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at android.os.Looper.loop(Looper.java:136)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at java.lang.reflect.Method.invokeNative(Native Method)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at java.lang.reflect.Method.invoke(Method.java:515)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at dalvik.system.NativeStart.main(Native Method)
02-28 06:38:52.308: E/AndroidRuntime(1125): Caused by: java.lang.NoSuchMethodException: multiply [class android.view.View]
02-28 06:38:52.308: E/AndroidRuntime(1125):     at java.lang.Class.getConstructorOrMethod(Class.java:472)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at java.lang.Class.getMethod(Class.java:857)
02-28 06:38:52.308: E/AndroidRuntime(1125):     at android.view.View$1.onClick(View.java:3803)
02-28 06:38:52.308: E/AndroidRuntime(1125):     ... 11 more

在我的Log Cat中发现问题我的方法Multiply应该接受一个参数View all thanks。

感谢。

2 个答案:

答案 0 :(得分:3)

首先在xml文件中为 EditText 定义inputType。

android:inputType="number"

并检查如下:

String errorMsg = "";
String str = edittext.getText().toString();
    if (checkInputAvailable(str)){
//do your work
}else{
Toast.makeText(context, ""+errorMsg, Toast.LENGTH_SHORT).show();
}
private boolean checkInputAvailable(String str) {
    boolean retVal = true;

    if (str.length() == 0) {
        errorMsg = "Enter at least a Number";
        retVal = false;
    } else if (!TextUtils.isDigitsOnly(str)) {
        errorMsg = "Enter a Number";
        retVal = false;
    }

    return retVal;

}

答案 1 :(得分:0)

问题只在于按钮的onClick方法。它意味着接收我忘记传入的View参数。所以代码应该是:

public void multiply(View v){
    final EditText start  = (EditText) findViewById(R.id.editText_InputStart);
    final EditText stop = (EditText) findViewById(R.id.editText_InputStop);
    String regEx = "[0-9]{1,3}";
    String str = start.getText().toString();
    String str1 = stop.getText().toString();

    if (str.length() != 0 && str.matches(regEx) && str1.length() != 0 && str1.matches(regEx)){
        calculate(str,str1);
                } else if(str.length() == 0 || !str.matches(regEx)) {
        start.setError("Enter a Number");
    } else if (str1.length() == 0 || !str1.matches(regEx)){
        stop.setError("Enter a Number");
    }

}