Two date pickers in the same android activity

I have activity when I need to accept two dates, a start date and an end date. I have two buttons that when clicked will display the date selection. After entering the date, I need to save this date using SharedPreferences. I am using this tutorial:

http://developer.android.com/guide/topics/ui/controls/pickers.html

I have seen questions on two date selections, but none of them use DialogFragment. They use obsolete functions, and I really don't want to use them, since I use DialogFragment better.

Now, how do I deal with TWO date pickers? How to find out which button was pressed (startButton or endButton) in the onDateSet function? Is there a way to save the view identifier in the DialogFragment dialog that I create when I click the button so that I can access it in onDateSet?

Any help would be great, thanks.

+4
source share
3 answers

This can be done as follows:

View.OnClickListener showDatePicker = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final View vv = v;

        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                if (vv.getId() == R.id.StartDate //id of your StartDate button) {
                        //do the stuff
                } else //EndDate button was clicked {
                        //do the stuff
                }
            }
        }, year, month, day);
        datePickerDialog.show();
    }
};
startDate.setOnClickListener(showDatePicker);
endDate.setOnClickListener(showDatePicker);

The main idea is to save the Viewlost OnClickEvent(in your buttons) and compare the ID of this view with the identifiers of your buttons

+10
source

, , , , , , , , :

class innerFirstDate implements DatePickerDialog.OnDateSetListener{
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            //Doing thing with first Date pick Dialog
        }

 }

class innerSecondDate implements DatePickerDialog.OnDateSetListener{
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            //Doing thing with second Date Picker Dialog
        }

 }

:

DatePickerDialog firstD = new DatePickerDialog(getActivity(), new innerFirstDate(), year, month, day).show();

:

DatePickerDialog secondD = new DatePickerDialog(getActivity(), new innerSecondDate(), year, month, day).show();

.

+1
public void onClick(View v) {
    d1= 1;
    d2=2;
    DatePickerDialog dialog = new DatePickerDialog(this, this, 2013, 2, 18);
    dialog.show();
}

public void onDateSet(DatePicker view, int year, int month, int day) {
    if(d1==1) {
        e2.setText(Integer.toString(day)+"/"+Integer.toString(month+1)+"/"+Integer.toString(year));
    }
    if(d2==1) {
        e3.setText(Integer.toString(day)+"/"+Integer.toString(month+1)+"/"+Integer.toString(year));
    }
}

public void onClick1(View v) {
    d2=1;
    d1=2;
    DatePickerDialog dialog = new DatePickerDialog(this, this,2013,2, 18);
    dialog.show();
}

works great

0
source

Source: https://habr.com/ru/post/1527558/


All Articles