如何防止用户在Android日期选择器中选择过去的日期

时间:2018-04-20 12:46:22

标签: android android-calendar android-datepicker

您好我正在制作一个Appointment app我正在使用 firebase数据库作为我的后端。

我想限制用户选择之前的日期。我得到了解决方案 from here

但我不知道在哪里放置代码才能使其正常工作。我将我的代码放在下面,请指导我同样的...

另一个问题是,如果用户更改了系统日期和时间,则datepicker中的日期也会更改有什么办法可以限制用户选择当前日期或明天的日期,但不是昨天的日期。

为前。获取ServerValue.TIMESTAMP并将其转换为日期,然后比较从TIMESTAMP获得的所选日期和日期。

Appointment.java

/**
 * A simple {@link Fragment} subclass.
 */
public class Appointment extends Fragment {

    @BindView(R.id.apointFirstName)
    EditText apointFirstName;
    @BindView(R.id.apointLastName)
    EditText apointLastName;
    @BindView(R.id.apointPhoneNumber)
    EditText apointPhoneNumber;
    @BindView(R.id.apointEmail)
    EditText apointEmail;
    @BindView(R.id.apointDate)
    EditText apointDate;
    @BindView(R.id.availTimeSpinner)
    Spinner availTimeSpinner;
    @BindView(R.id.bookAppointmentButton)
    Button book_btn;
    @BindView(R.id.cancelAppointmentButton)
    Button cancel_btn;

    Calendar mCal;
    int day, month, year;
    String mCurrentDate;
    String fname,lname,email;
    int phone;
    String timestamp = String.valueOf(ServerValue.TIMESTAMP);

    public Appointment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_appointment, container, false);
        ButterKnife.bind(this,view);


        setCurrentDate();

        apointDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectDate();
            }
        });
        return view;
    }

    // set the current date on the text view
    private void setCurrentDate() {

        mCal = Calendar.getInstance();

        day = mCal.get(Calendar.DAY_OF_MONTH);
        month = mCal.get(Calendar.MONTH);
        year = mCal.get(Calendar.YEAR);

        month = month +1;

        mCurrentDate = (day+"/"+month+"/"+year);

    }

    //open the datepickerdialogue
    private void selectDate() {
        if(getActivity()!=null) {
            DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    month = month + 1;
                    apointDate.setText(dayOfMonth + "/" + month + "/" + year);
                }
            }, year, month, day);


            datePickerDialog.show();
        }
    }

}

2 个答案:

答案 0 :(得分:3)

添加以下行用户无法选择旧数据..

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

答案 1 :(得分:2)

使用 datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); 在日期选择器中禁用粘贴日期

试试这个

private void selectDate() {
        if(getActivity()!=null) {
            DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    month = month + 1;
                    apointDate.setText(dayOfMonth + "/" + month + "/" + year);
                }
            }, year, month, day);

            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            datePickerDialog.show();
        }
    }
  

用户更改系统日期和时间,datepicker中的日期也会发生变化,是否可以限制用户选择当前日期或明天

检查此链接

  1. Get Real Time - Not Device Set Time in android
  2. Android - Get current time without dependency on device's clock
  3. How to get device independent local date and time Android