验证日期+时间选择器

时间:2017-11-21 15:27:59

标签: java android validation jodatime

我正在打磨我的应用程序并尝试确保它不会被破坏。我有一个CreateLine页面,最后有一个保存按钮。我正在进行测试并尝试在选择日期或时间选择器之前单击“保存”按钮来破坏我的应用程序并且应用程序崩溃。基本上我想要发生的是它显示一个警告对话框,说出“确保已输入所有字段”之类的内容。

问题是我正在使用JODA-TIME而且我不太确定如何检查或验证这个?我在下面添加了我的拾取器和保存按钮的代码:

日期+时间选择器:

 datePickerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            DatePickerDialog datePickerDialog = new DatePickerDialog(CreateLine.this, new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {


                    monthOfYear = monthOfYear + 1;
                    String Sd = String.valueOf(dayOfMonth + "/" + monthOfYear + "/" + year);

                    datePickerButton.setText(Sd);

                    yearNow = year;
                    monthNow = monthOfYear;
                    dayNow = dayOfMonth;

                }


            }, displayCurrentYear, displayCurrentMonth, displayCurrentDay);


            datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
            datePickerDialog.show();
        }


    });

    timePickerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            TimePickerDialog timePickerDialog = new TimePickerDialog(CreateLine.this, new TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker timePicker, int hour, int minute) {

                    hourNow = hour;
                    minuteNow = minute;

                    String St = String.format("%02d:%02d", hour, minute);
                    timePickerButton.setText(St);



                }
            }, displayCurrentHour, displayCurrentMinute, isTwentyFourHrsNow);

            timePickerDialog.show();
        }
    });

保存按钮:

VerifyButton.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {

            if (spinner.getSelectedItem().toString().equals("Select Line Type") || spinner2.getSelectedItem().toString().equals("Select Package Type")) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(CreateLine.this);
                    builder.setTitle("Data Input Error");
                    builder.setMessage("Please Ensure that all fields have been selected before Saving!");
                    builder.setIcon(R.drawable.warning_icon);
                    builder.setCancelable(true);

                    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    AlertDialog alert = builder.create();
                    alert.show();
                } else if (monthNow >= 1 && monthNow <= 12) {

                    spinnerSelection = String.valueOf(spinner.getSelectedItem());
                    spinnerSelection2 = String.valueOf(spinner2.getSelectedItem());
                    String q = quantity.getText().toString();
                    String d = duration.getText().toString();

                    DateTime datetime = new DateTime(yearNow, monthNow, dayNow, hourNow, minuteNow);
                    DateTimeFormatter fmt = DateTimeFormat.forPattern("EE dd MM yyyy" + "\n" + " h:mm a ");
                    String formattedtime = fmt.print(datetime);
                    CalculateButton.setText(formattedtime);

                    // Plus some hours, minutes, and seconds to the original DateTime.
                    DateTimeFormatter fmt2 = DateTimeFormat.forPattern("EE dd MM yyyy" + "\n" + " h:mm a ");

                    DateTime dateTime1 = datetime.plusHours(timeadded);
                    String endtimecalc = fmt2.print(dateTime1);
                    TextView endtime = findViewById(endtimetextView);
                    endtime.setVisibility(View.VISIBLE);
                    endtime.setText(endtimecalc);

                    //INSERT DATA TO DATABASE
                    boolean isInserted = myDb.insertData(
                            spinnerSelection,
                            spinnerSelection2,
                            q,
                            d,
                            formattedtime,
                            endtimecalc);


                    if (isInserted == true) {
                        Toast.makeText(CreateLine.this, "Data Inserted Successfully", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(CreateLine.this, Dashboard.class);
                        startActivity(intent);
                    } else if (isInserted == false) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(CreateLine.this);
                        builder.setTitle("Save Data Error");
                        builder.setMessage("Data for this Line already exists.." + "\n" + "Please Clear Line First!");
                        builder.setIcon(R.drawable.warning_icon);
                        builder.setCancelable(true);

                        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(CreateLine.this, Dashboard.class);
                                startActivity(intent);
                            }
                        });

                        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                        AlertDialog alert = builder.create();
                        alert.show();
                    } else {
                        AlertDialog.Builder builder = new AlertDialog.Builder(CreateLine.this);
                        builder.setTitle("Save Data Error");
                        builder.setMessage("No Data has been entered!");
                        builder.setIcon(R.drawable.warning_icon);
                        builder.setCancelable(true);

                        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });

                        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                        AlertDialog alert = builder.create();
                        alert.show();
                    }
                }
        }
    });
}

}

以下是我收到的Logcat错误:

  

org.joda.time.IllegalFieldValueException:monthOfYear的值0必须在[1,12]范围内                         在org.joda.time.field.FieldUtils.verifyValueBounds(FieldUtils.java:275)                         在org.joda.time.chrono.BasicChronology.getDateMidnightMillis(BasicChronology.java:631)                         at org.joda.time.chrono.BasicChronology.getDateTimeMillis0(BasicChronology.java:186)                         at org.joda.time.chrono.BasicChronology.getDateTimeMillis(BasicChronology.java:182)                         at org.joda.time.chrono.AssembledChronology.getDateTimeMillis(AssembledChronology.java:133)                         在org.joda.time.chrono.ZonedChronology.getDateTimeMillis(ZonedChronology.java:122)                         at org.joda.time.chrono.AssembledChronology.getDateTimeMillis(AssembledChronology.java:133)                         在org.joda.time.base.BaseDateTime。(BaseDateTime.java:257)                         在org.joda.time.base.BaseDateTime。(BaseDateTime.java:198)                         在org.joda.time.DateTime。(DateTime.java:323)                         在com.almac.tracker.CreateLine $ 6.onClick(CreateLine.java:313)

LINE 313:

DateTime datetime = new DateTime(yearNow,monthNow,dayNow,hourNow,minuteNow);

有谁知道如何检查或验证这个?

1 个答案:

答案 0 :(得分:0)

VerifyButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        if(monthNow>=1 && monthNow <=12){
            doWhatYouDoNow;
        } else {
            alert(errorMsg);
        }
    }
}

或者不只是检查月份,您可以制作一个方法来验证所有参数并返回错误消息的映射,然后显示给用户。