如何不允许用户选择过去的日期,日期选择器? [英] how to not allow user select past date in datepicker?

查看:114
本文介绍了如何不允许用户选择过去的日期,日期选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做,当用户选择过去的日期,日期选择器,它不应该接受昨天的日期。我的应用程序将提示信息,告知用户不能选择低于当前date.anyone指导我好吗?......谢谢

  mDateDisplay =(TextView中)findViewById(R.id.dateDisplay);
            按钮pickDate_btn =(按钮)findViewById(R.id.pickDate);
            pickDate_btn.setOnClickListener(新View.OnClickListener(){

            公共无效的onClick(视图v){
                的ShowDialog(DATE_DIALOG_ID);
            }
        });

        最后的日历现在= Calendar.getInstance();
        mYear = now.get(Calendar.YEAR);
        mMonth = now.get(Calendar.MONTH);
        MDAY = now.get(Calendar.DAY_OF_MONTH);

        updateDisplay();

@覆盖
    受保护的对话框onCreateDialog(INT ID){
        开关(ID){
            案例DATE_DIALOG_ID:
                返回新DatePickerDialog(这一点,
                            mDateSetListener,
                            mYear,mMonth,MDAY);
        }
        返回null;
    }

    @覆盖
    在prepareDialog(INT ID,对话对话框){保护无效
        开关(ID){

            案例DATE_DIALOG_ID:
                ((DatePickerDialog)对话框).updateDate(mYear,mMonth,MDAY);
                打破;
        }
    }

    私人无效updateDisplay(){
        mDateDisplay.setText(
            新的StringBuilder()
                    //月份是0基于所以加1
                    .append(MDAY).append( - )
                    .append(mMonth + 1).append( - )
                    .append(mYear).append());
    }

    私人DatePickerDialog.OnDateSetListener mDateSetListener =
            新DatePickerDialog.OnDateSetListener(){

    @覆盖
    公共无效onDateSet(DatePicker的观点,年整型,诠释monthOfYear,诠释DAYOFMONTH){
                    // TODO自动生成方法存根

            mYear =年;
                    mMonth = monthOfYear;
                    MDAY = DAYOFMONTH;
                    updateDisplay();

    //应该在这儿加上code?
                }
            };
 

解决方案

我们不能将它限制的DatePicker选择哪个不是过去的某个日期的任何属性,但是我们可以通过编程做到这一点,方法是:

 如果(dateObj1.before(dateObj2)|| dateObj1.equals(dateObj2)){
//该程序正常运行
}
其他{

                新AlertDialog.Builder(PM_Edit.this)

                .setTitle(错误数据输入!)

                .setMessage(结束日期必须在开始日期之前,请插入新的日期值)

                .setNeutralButton(OK,

                新DialogInterface.OnClickListener(){

                公共无效的onClick(DialogInterface对话框,

                其中INT){

                }

                })。显示();
            }
 

I would like to do when user select the past date in datepicker, it should not accept yesterday date. My application will prompt message to inform user cant select less than current date.anyone guide me please ?...Thank you

mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
            Button pickDate_btn = (Button) findViewById(R.id.pickDate);
            pickDate_btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });

        final Calendar now = Calendar.getInstance();
        mYear = now.get(Calendar.YEAR);
        mMonth = now.get(Calendar.MONTH);
        mDay = now.get(Calendar.DAY_OF_MONTH);

        updateDisplay();      

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                            mDateSetListener,
                            mYear, mMonth, mDay);
        }
        return null;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {

            case DATE_DIALOG_ID:
                ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
                break;
        }
    }    

    private void updateDisplay() {
        mDateDisplay.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mDay).append("-")
                    .append(mMonth + 1).append("-")
                    .append(mYear).append(" "));
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
                    // TODO Auto-generated method stub

            mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();

    //should add code here?             
                }
            };

解决方案

We can not set any attribute which limits DatePicker to select a date which is not past, but we can do it programmatically, by:

if(dateObj1.before(dateObj2) || dateObj1.equals(dateObj2)){
//the program runs normally
}
else{

                new AlertDialog.Builder(PM_Edit.this)

                .setTitle("Wrong Data Input!")

                .setMessage("The end Date must be Before the start Date, please insert new Date values")

                .setNeutralButton("Ok",

                new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog,

                int which) {

                }

                }).show();
            }

这篇关于如何不允许用户选择过去的日期,日期选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆