验证日期时间选择器

时间:2016-05-04 12:16:59

标签: java android datetime

嘿伙计们我有文本视图,当用户点击每个文本视图时,日期选择器会显示出来。我在同一页面上有八个文本视图,它们按照时间顺序排序。现在的挑战是我想要的第一个日期时间选择器显示从今天开始的日期,下一个日期时间选择器应该只显示大于第一个文本视图中选择的日期的日期。第三个也应该显示的日期大于在第二个文本视图中选择的日期。我当前的代码是

public class  General extends Fragment {
        TextView onlinee, orientationn, semesterstarts, semesterbreakstarts, semesterbreakends, examstarts, examends, semesterends;
        Button btncalendarr;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_general, container, false);
        onlinee = (TextView)rootView.findViewById(R.id.grestarts);
        orientationn=(TextView) rootView.findViewById(R.id.gorstart);
        semesterstarts= (TextView) rootView.findViewById(R.id.gstart);
        semesterbreakstarts = (TextView)rootView.findViewById(R.id.semstart);
        semesterbreakends = (TextView) rootView.findViewById(R.id.semend);
        examstarts= (TextView) rootView.findViewById(R.id.exstart);
        examends= (TextView) rootView.findViewById(R.id.exend);
        semesterends=(TextView) rootView.findViewById(R.id.semsend);
        btncalendarr = (Button) rootView.findViewById(R.id.btncalendar);
        onlinee.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Calendar mcurrentDate = Calendar.getInstance();
                int mYear = mcurrentDate.get(Calendar.YEAR);
                int mMonth = mcurrentDate.get(Calendar.MONTH);
                int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog mDatePicker;
                mDatePicker = new DatePickerDialog(General.this.getActivity(), new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                    // TODO Auto-generated method stub
                    /*      Your code   to get date and time    */
                        selectedmonth = selectedmonth + 1;
                        onlinee.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
                    }
                    }, mYear, mMonth, mDay);
                    mDatePicker.setTitle("Select Date");
                    mDatePicker.show();
                 }
        });
        orientationn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        Calendar mcurrentDate = Calendar.getInstance();
        int mYear = mcurrentDate.get(Calendar.YEAR);

        int mMonth = mcurrentDate.get(Calendar.MONTH);
        int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog mDatePicker;
        mDatePicker = new DatePickerDialog(General.this.getActivity(), new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
            // TODO Auto-generated method stub
                /*      Your code   to get date and time    */
            selectedmonth = selectedmonth + 1;
            orientationn.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
        }
        }, mYear, mMonth, mDay);
        mDatePicker.setTitle("Select Date");
        mDatePicker.show();
            }
        });
        semesterstarts.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        Calendar mcurrentDate = Calendar.getInstance();
        int mYear = mcurrentDate.get(Calendar.YEAR);
        int mMonth = mcurrentDate.get(Calendar.MONTH);
        int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog mDatePicker;
        mDatePicker = new DatePickerDialog(General.this.getActivity(), new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
            // TODO Auto-generated method stub
                /*      Your code   to get date and time    */
            selectedmonth = selectedmonth + 1;
            semesterstarts.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
            }
        }, mYear, mMonth, mDay);
        mDatePicker.setTitle("Select Date");
        mDatePicker.show();
        }
                    });
        semesterbreakstarts.setOnClickListener(new View.OnClickListener() {
            @Override
        public void onClick(View view) {
        Calendar mcurrentDate = Calendar.getInstance();
        int mYear = mcurrentDate.get(Calendar.YEAR);
        int mMonth = mcurrentDate.get(Calendar.MONTH);
        int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog mDatePicker;
        mDatePicker = new DatePickerDialog(General.this.getActivity(), new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                // TODO Auto-generated method stub
                    /*      Your code   to get date and time    */
                selectedmonth = selectedmonth + 1;
                semesterbreakstarts.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
            }
            }, mYear, mMonth, mDay);
            mDatePicker.setTitle("Select Date");
            mDatePicker.show();
        }
        });
            semesterbreakends.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             Calendar mcurrentDate = Calendar.getInstance();
             int mYear = mcurrentDate.get(Calendar.YEAR);
             int mMonth = mcurrentDate.get(Calendar.MONTH);
             int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

             DatePickerDialog mDatePicker;
             mDatePicker = new DatePickerDialog(General.this.getActivity(), new DatePickerDialog.OnDateSetListener() {
                 public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                     // TODO Auto-generated method stub
                /*      Your code   to get date and time    */
                     selectedmonth = selectedmonth + 1;
                     semesterbreakends.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
                 }
             }, mYear, mMonth, mDay);
             mDatePicker.setTitle("Select Date");
             mDatePicker.show();
         }
     });
        examstarts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            Calendar mcurrentDate = Calendar.getInstance();
            int mYear = mcurrentDate.get(Calendar.YEAR);
            int mMonth = mcurrentDate.get(Calendar.MONTH);
            int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog mDatePicker;
            mDatePicker = new DatePickerDialog(General.this.getActivity(), new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                    // TODO Auto-generated method stub
                        /*      Your code   to get date and time    */
                    selectedmonth = selectedmonth + 1;
                    examstarts.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
                }
            }, mYear, mMonth, mDay);
            mDatePicker.setTitle("Select Date");
            mDatePicker.show();
            }
            });

            examends.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Calendar mcurrentDate = Calendar.getInstance();
                    int mYear = mcurrentDate.get(Calendar.YEAR);
                    int mMonth = mcurrentDate.get(Calendar.MONTH);
                    int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog mDatePicker;
                mDatePicker = new DatePickerDialog(General.this.getActivity(), new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                        // TODO Auto-generated method stub
                            /*      Your code   to get date and time    */
                        selectedmonth = selectedmonth + 1;
                        examends.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
                    }
                }, mYear, mMonth, mDay);
                mDatePicker.setTitle("Select Date");
                mDatePicker.show();
                }
                    });
                     semesterends.setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View view) {
             Calendar mcurrentDate = Calendar.getInstance();
             int mYear = mcurrentDate.get(Calendar.YEAR);
             int mMonth = mcurrentDate.get(Calendar.MONTH);
             int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);

             DatePickerDialog mDatePicker;
             mDatePicker = new DatePickerDialog(General.this.getActivity(), new DatePickerDialog.OnDateSetListener() {
                 public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                     // TODO Auto-generated method stub
                /*      Your code   to get date and time    */
                     selectedmonth = selectedmonth + 1;
                                 semesterends.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
                             }
                         }, mYear, mMonth, mDay);
                         mDatePicker.setTitle("Select Date");
                         mDatePicker.show();
                     }
                 });
                 btncalendarr.setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View view) {

             String online = onlinee.getText().toString();
             String orientation = orientationn.getText().toString();
             String simstart= semesterstarts.getText().toString();
             String simbstart = semesterbreakstarts.getText().toString();
             String sembreak = semesterbreakends.getText().toString();
             String examstart = examstarts.getText().toString();
             String examend = examends.getText().toString();
             String semmend = semesterends.getText().toString();
             if (online.isEmpty()||orientation.isEmpty()||simstart.isEmpty()||simbstart.isEmpty()||sembreak.isEmpty()||examstart.isEmpty()||examend.isEmpty()||semmend.isEmpty()){
                 android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(General.this.getActivity());
                 builder.setMessage("PLEASE FILL ALL FIELDS");
                 builder.setTitle("INPUT ERROR");
                 builder.setPositiveButton(android.R.string.ok, null);
                 android.app.AlertDialog dialog = builder.create();
                 dialog.show();
             }
             else {
                 new BackgroundTask(General.this.getActivity()).execute(online, orientation, simstart, simbstart, sembreak, examstart, examend, semmend);
              //   onlinee.setText("");
              //   orientationn.setText("");
               ///  semesterstarts.setText("");
               //  semesterbreakstarts.setText("");
                // semesterbreakends.setText("");
                /// examstarts.setText("");
                // examends.setText("");
                // semesterends.setText("");
                }
            }
        });
        return rootView;
    }
}

1 个答案:

答案 0 :(得分:0)

我没有清楚地得到你的问题。但是为什么不使用this link设置日期选择器的最小日期。 你可以通过计算你的日期到毫秒然后比较两毫秒来做到这一点。 见link

如果我理解正确的话,那可能会有所帮助。