安卓的DatePicker日期限制日期、DatePicker

2023-09-04 05:27:42 作者:暴走的小菇凉

我使用DatePicket在我的活动,

I am using DatePicket in my activity ,

我要限制用户挑今天的日期。

I want to limit the date picked by user to todays date.

他们不应该能够选择日期大于今天的日期。

They should not able to select date greater than todays date.

感谢你。

推荐答案

是的,你可以做到这一点很easely这里的验证是为例:

yes you can do it very easely the validation here is the exemple:

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();
            }

贷: http://www.brighthub.com/mobile/google-android/articles/41545.aspx