如何使用Datepicker禁用将来的日期

时间:2014-04-16 12:07:14

标签: android date datepicker

我正在尝试为将来的日期禁用日期选择器,但却不知道......

以下是我尝试的内容:

 bday.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


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


                DatePickerDialog   mDatePicker =new DatePickerDialog(CreateAccountActivity.this, new OnDateSetListener()
                {   
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) 
                    {
                            year = selectedyear; 
                            month = selectedmonth; 
                            day = selectedday;

                            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");      
                            Calendar cal = Calendar.getInstance();


                            String currenttime = new String(dateFormat.format(cal.getTime()));
                            String selectedtime = new String (new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                            String futuretime = new String (dateFormat.format(System.currentTimeMillis()));      

                           if(selectedtime==currenttime)
                        { 
                            Toast.makeText(getApplicationContext(), "you were not born in the future", Toast.LENGTH_SHORT).show();
                            bday.setText("");
                        }
                        else
                        {
                     bday.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                    }}
                },year, month, day);
                mDatePicker.setTitle("Please select date");                
                mDatePicker.show();
        }
    });
建议使用

Stackoverflow

mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

但在上述情况下我应该如何使用它。 我想禁用将来的日期。但是没有任何想法:(

2 个答案:

答案 0 :(得分:1)

在声明听众后 DatePicker使用mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

答案 1 :(得分:1)

package com.keshav.datePicker_With_Hide_Future_Past_Date;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    EditText ed_date;
    int year;
    int month;
    int day;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed_date=(EditText) findViewById(R.id.et_date);

        ed_date.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                final DatePickerDialog   mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
                {
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
                    {
                              ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                            int month_k=selectedmonth+1;

                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");
                // TODO Hide Future Date Here
                mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

                // TODO Hide Past Date Here
                //  mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
                mDatePicker.show();
            }
        });
    }
}