Android DatePicker只允许选择昨天的日期,月和年不可更改

Android DatePicker allows only yesterday's date to be selected, Month and Year not changeable

我有一个日期选择器,它应该允许用户只选择昨天的日期,并且月份和年份不应更改。以下是我的日期选择器代码,有人可以查看它并告诉我要进行的更改以达到我想要的结果吗?

日期选择器Java:

public class MainActivity extends ActionBarActivity {

    java.text.DateFormat format = java.text.DateFormat.getDateInstance();
    Calendar cal  = Calendar.getInstance(); 
    TextView label;
    Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        label = (TextView) findViewById(R.id.textView1);
        btn = (Button) findViewById(R.id.button1);
        updatedate();

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                setDate();
            }
        });
    }



    public void updatedate(){
        label.setText(format.format(cal.getTime()));
    }

    public void setDate(){

        new DatePickerDialog(MainActivity.this, d, cal.get(cal.YEAR), cal.get(cal.MONTH), cal.get(cal.DAY_OF_MONTH)).show();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {

            cal.set(cal.YEAR, year);
            cal.set(cal.MONTH, monthOfYear);
            cal.set(cal.DAY_OF_MONTH, dayOfMonth);
            updatedate();
        }
    };

}
 Say for eg:

您在此处使用按钮单击并打开日期选择器对话框,选择后您将在文本视图中设置它们。

   dateOfBirth = (Button) findViewById(R.id.dateOfBirth);

dateOfBirth.setOnClickListener(onDatePickerClick);

    private final OnClickListener onDatePickerClick = new OnClickListener() {

    @Override
    public void onClick(View v) {
        Utils.getDatePickerDialog(PersonalDetails.this, datePickerListener,
                birthDayValue, birthMonthValue, birthYearValue);
                  //Utils is a class file where i used a method getDatePickerDialog
    }
   private final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            String birthDayDateFormt = Utils.getBirthDayDate(
                        selectedYear, selectedMonth, selectedDay);
                              textView.setText(birthDayDateFormt);

}

Utils:

 public static void getDatePickerDialog(Activity activity, DatePickerDialog.OnDateSetListener datePickerListener,int selectedDay,int selectedMonth, int selectedYear) {

    Calendar c = Calendar.getInstance();
    int year,month,day=0; 
    if(selectedYear==0 && selectedMonth ==0 && selectedDay ==0)
    {
         year = c.get(Calendar.YEAR);
         month = c.get(Calendar.MONTH);
         day = c.get(Calendar.DAY_OF_MONTH);
    }
    else
    {
        year = selectedYear;
         month = selectedMonth;
         day = selectedDay;
    }
    Date minDate = new Date();
    c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH) - 1, 0, 0);
    minDate.setTime(c.getTimeInMillis());
    DatePickerDialog dpdFromDate = new DatePickerDialog(activity, datePickerListener, year, month, day);
    dpdFromDate.getDatePicker().setMaxDate(System.currentTimeMillis()); .. here i used maxdate.
    dpdFromDate.show();

}
  public static String getBirthDayDate(int selectedYear, int selectedMonth,
        int selectedDay) {
    int month = selectedMonth + 1;
    String formattedMonth = Constants.EMPTY + month;
    String formattedDayOfMonth = Constants.EMPTY + selectedDay;
    if (month < 10) {
        formattedMonth = Constants.ZERO + month;
    }if (selectedDay < 10) {
        formattedDayOfMonth = Constants.ZERO + selectedDay;
    }
    return String.valueOf(formattedMonth)+"/"+String.valueOf(formattedDayOfMonth)+"/"+String.valueOf(selectedYear);
}

我不知道这是否是标准做法,但按照我的逻辑,我通过以下代码得到了我想要的结果。它会让用户 select 只有昨天的日期。

例如:如果日期是 14/02/2015 并且用户 selects 14/02/2015 或 13/02/2015 或 12/02/2015 日期将设置为 13/仅限 02/2015

基本上这意味着日期将设置为昨天的日期,年份为当前年份,月份为当前。

JAVA:

import java.util.Calendar;
import java.util.Date;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    java.text.DateFormat format = java.text.DateFormat.getDateInstance();
    Calendar cal = Calendar.getInstance();
    TextView label;
    Button btn;
    int y, m , day;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        label = (TextView) findViewById(R.id.textView1);
        btn = (Button) findViewById(R.id.button1);
        updatedate();



        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                setDate();



            }
        });
    }

    public void updatedate() {

        //cal.add(Calendar.DATE, -1);
        System.out.println("date format: "+format.format(cal.getTime()));
        label.setText(format.format(cal.getTime()));
    }

    public void setDate() {

        DatePickerDialog de = new DatePickerDialog(MainActivity.this, d,cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
        de.getDatePicker().setMaxDate(System.currentTimeMillis());



        de.show();

    }

    DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {


            y = Calendar.getInstance().get(Calendar.YEAR);
            m = Calendar.getInstance().get(Calendar.MONTH);
            day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);

            if(day==dayOfMonth)
            {
                int dt = dayOfMonth-1;
                cal.set(Calendar.DAY_OF_MONTH, dt );    
            }
            else if(day>dayOfMonth)
            {
                int ldt = day-1;
                cal.set(Calendar.DAY_OF_MONTH, ldt);
            }

            if(year<y)// && monthOfYear<m && day==dayOfMonth){
            {
                //int dt = dayOfMonth-1;
                year = y;
                //monthOfYear = m;
                //dayOfMonth = day;
                //cal.set(Calendar.MONTH, monthOfYear);
                cal.set(Calendar.YEAR, year);
                //cal.set(Calendar.DAY_OF_MONTH, dt);               

            }

            if(monthOfYear<m)
            {
                monthOfYear = m;
                cal.set(Calendar.MONTH, monthOfYear);
            }






            updatedate();


        }

    };

}