如何使用输入验证类?

时间:2019-06-05 12:46:32

标签: android

我正在创建一个应用程序,其中创建了一个验证类,但是当我运行这些代码时,它给我一个错误,所以我不知道这是否是在类内部进行验证的正确方法,可以谁来帮帮我。

这是 InputValidation类

的源代码
    public class InputValidation {

// Show Validation

 if (validate()){

    // Get values from edit text fields.

    String GoingAbroad = GoingAbroad.getText().toString();
    String types = types.getSelectedItem().toString();
    String leaves = Leaves.getSelectedItem().toString();
    String noOfDays = noOfDays.getText().toString();
    String reason = reason.getText().toString();
    String startDate = startDate.getText().toString();
    String endDate = endDate.getText().toString();
}
// This method is used to validate input given by user
private boolean validate() {
    boolean valid = false;
    // Get values from editText fields
    String GoingAbroad = GoingAbroad.getText().toString();
    String types = types.getSelectedItem().toString();
    String leaves = Leaves.getSelectedItem().toString();
    String noOfDays = noOfDays.getText().toString();
    String reason = reason.getText().toString();
    String startDate = startDate.getText().toString();
    String endDate = endDate.getText().toString();

    // Handling validation for User Name field.

    if (leaves.isEmpty()){
        valid = false;
        Leaves.setError("Please Choose your Type of Leave No 1");

    } else {
        valid = true;
        Leaves.setError(null);
    }


    if (noOfDays.isEmpty()) {
        valid = false;
        noOfDays.setError("Please enter the No. days for Leave Type 1");

    } else {
        valid = true;
        noOfDays.setError(null);
    }
    if (startDate.isEmpty()) {
        valid = false;
        startDate.setError("Please Enter The Expected Date Where you want to start Your leave");

    } else {

        valid = true;
        startDate.setError(null);
    }
    if (endDate.isEmpty()) {
        valid = false;
        endDate.setError("Please Enter the Expected Date Where you want to End Your leave");

    } else {
        valid = true;
        endDate.setError(null);
    }

    if (types.isEmpty()) {
        valid = false;
        types.setError("Please enter the Payment option you want");

    } else {
        valid = true;
        types.setError(null);
    }

    // Check validation for end date that is greater or less than start date
    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");

    try {
        if(sdf.parse(startDate).before(sdf.parse(startDate)))
        {
            valid = true;//If start date is before end date
            startDate.setError(null);

        }
        else if(sdf.parse(startDate).equals(sdf.parse(endDate)))
        {
            valid = false;//If two dates are equal
            startDate.setError("The start date should not be same with end date");
            endDate.setError("The End Date should not be same as Start Date");
        }
        else if (sdf.parse(startDate).before(sdf.parse(endDate)))
        {
            valid = false; //If start date is after the end date
            endDate.setError("The End Date should not be less than Start Date");

        }
    } catch (Exception e) {
        e.printStackTrace();
    }


    return valid;
}

这就是给我的: enter image description here

我遇到了(错误:预期的类,接口或枚举),我很难解决此错误。请帮忙!

0 个答案:

没有答案