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

时间:2015-02-13 07:46:42

标签: android datepicker

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

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

}

2 个答案:

答案 0 :(得分:1)

 Say for eg:

您正在使用此处的按钮单击并打开日期选择器对话框,并在选择后在textview中设置它们。

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

答案 1 :(得分:0)

我不知道这是否是标准做法,但根据我的逻辑,我通过以下代码得到了我想要的结果。它将让用户只选择昨天的日期。

例如:如果日期是2015年2月14日且用户选择2015年2月14日或2015年2月13日或2015年2月12日,则日期将仅设为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();


        }

    };

}