web-dev-qa-db-fra.com

Comment désactiver les dates avant la date d'aujourd'hui dans DatePickerDialog Android?

Je veux désactiver les dates avant la date d'aujourd'hui dans le DatePickerDialog. Je suis nouveau dans Android veuillez me suggérer comment pourrais-je faire cela. Voici mon code que j'ai écrit pour DatePickerDialog

 final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                    myCalendar.set(Calendar.YEAR, year);
                    myCalendar.set(Calendar.MONTH, monthOfYear);
                    myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    updateLabel(val);
            }
        };
        depart.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    new DatePickerDialog(this, date, myCalendar
                            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                    val=1;
                }
            });

        returnDate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    new DatePickerDialog(this, date, myCalendar
                            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                            val=2;
                }
            });


private void updateLabel(int val) {

        String myFormat = "dd/MM/yy"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        Log.d("Date vlue ", "==="+sdf.format(myCalendar.getTime()));
        if(val==1)
            depart.setText(sdf.format(myCalendar.getTime()));
        else
            returnDate.setText(sdf.format(myCalendar.getTime()));

        }

Veuillez me suggérer ce que vous devez faire

11
Developer

Voir cet exemple ..!

import Java.util.Calendar;
import Android.app.Activity;
import Android.app.DatePickerDialog;
import Android.app.Dialog;
import Android.os.Bundle;
import Android.view.View;
import Android.view.View.OnClickListener;
import Android.widget.Button;
import Android.widget.DatePicker;
import Android.widget.TextView;

public class MyAndroidAppActivity extends Activity {

    private TextView tvDisplayDate;

    private Button btnChangeDate;

    private int myear;
    private int mmonth;
    private int mday;

    static final int DATE_DIALOG_ID = 999;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setCurrentDateOnView();
        addListenerOnButton();

    }

    // display current date
    public void setCurrentDateOnView() {

        tvDisplayDate = (TextView) findViewById(R.id.tvDate);       

        final Calendar c = Calendar.getInstance();
        myear = c.get(Calendar.YEAR);
        mmonth = c.get(Calendar.MONTH);
        mday = c.get(Calendar.DAY_OF_MONTH);

        // set current date into textview
        tvDisplayDate.setText(new StringBuilder()
                // Month is 0 based, just add 1
                .append(mmonth + 1).append("-").append(mday).append("-")
                .append(myear).append(" "));
    }

    public void addListenerOnButton() {

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

        btnChangeDate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                showDialog(DATE_DIALOG_ID);

            }

        });

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            DatePickerDialog _date =   new DatePickerDialog(this, datePickerListener, myear,mmonth,
                    mday){
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {   
                    if (year < myear)
                        view.updateDate(myear, mmonth, mday);

                    if (monthOfYear < mmonth && year == myear)
                        view.updateDate(myear, mmonth, mday);

                    if (dayOfMonth < mday && year == myear && monthOfYear == mmonth)
                        view.updateDate(myear, mmonth, mday);

                }
            };
            return _date;
        }
        return null;
    }

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

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            myear = selectedYear;
            mmonth = selectedMonth;
            mday = selectedDay;

            // set selected date into textview
            tvDisplayDate.setText(new StringBuilder().append(mmonth + 1)
                    .append("-").append(mday).append("-").append(myear)
                    .append(" "));    

        }
    };

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:orientation="vertical" >

    <Button
        Android:id="@+id/btnChangeDate"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:text="Change Date" />

    <TextView
        Android:id="@+id/lblDate"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:text="Current Date (M-D-YYYY): "
        Android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        Android:id="@+id/tvDate"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:text=""
        Android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
8
Hariharan

Obtenez le DatePicker utilisé dans votre DatePickerDialog avec la méthode getDatePicker () et utilisez la méthode setMinDate (Long millis) .

Donnez-lui la date minimale (en millisecondes de l'époque) que vous devez définir.

Vous pouvez donc faire quelque chose comme

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date d = sdf.parse("21/12/2012");
DatePicker datePicker = date.getDatePicker();
datePicker.setMinDate(d.getTime());

MODIFIER:

ok, donc lorsque vous créez votre DatePickerDialog, avant de le montrer, enregistrez-le simplement dans une variable, définissez la date minimale, puis affichez-le.

DatePickerDialog dpd = new DatePickerDialog(IweenFlightSearch.this, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date d = sdf.parse("21/12/2012");
dpd.getDatePicker().setMinDate(d.getTime());
dpd.show();

cela devrait fonctionner.

20
Enrichman

// Créer une nouvelle instance de DatePickerDialog et la renvoyer

 DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this,year,month,day);
            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
            return datePickerDialog;
6
Dhara Patel

Je sais qu'il est trop tard. Mais j'espère que cela vous aidera. Utilisation

date .getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

dans votre cas, la date est DatePickerDialog

À votre santé!

4
KZoNE

J'ai obtenu la solution. J'espère que cela aidera quelqu'un statique final int DATE_DIALOG_ID = 999;

depart.setOnClickListener(new View.OnClickListener() {

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

                }
            });



protected Dialog onCreateDialog(int id) {
        final Calendar now = Calendar.getInstance();

        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            DatePickerDialog _date =   new DatePickerDialog(this, date,myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)){
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {   
                    if (year < now.get(Calendar.YEAR))

                        view.updateDate(myCalendar
                                .get(Calendar.YEAR), myCalendar
                                .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));

                    if (monthOfYear < now.get(Calendar.MONTH) && year == now.get(Calendar.YEAR))
                        view.updateDate(myCalendar
                                .get(Calendar.YEAR), myCalendar
                                .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));

                    if (dayOfMonth < now.get(Calendar.DAY_OF_MONTH) && year == now.get(Calendar.YEAR) && 
                            monthOfYear == now.get(Calendar.MONTH))
                        view.updateDate(myCalendar
                                .get(Calendar.YEAR), myCalendar
                                .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
                        }
            };
            return _date;
        }
        return null;
    }
2
Developer