Подтвердить что ты не робот

Множество DatePickers в одной активности

Я абсолютно новичок в платформе Android и создаю приложение, изучая процесс разработки.

В настоящее время я работаю над тем, что мне нужно развернуть 2 выбора даты. Первая - "Дата начала", а другая - "Дата окончания". Я слежу за учебником DatePicker на странице разработчиков Android: http://developer.android.com/resources/tutorials/views/hello-datepicker.html

Для одного DatePicker он работает нормально.

Теперь моя проблема заключается в том, что, когда я реплицирую весь процесс для второго выбора даты, он отображается как раз как на эмуляторе, так и на телефоне. Но когда независимо от того, какую кнопку я нажимаю для выбора дат, обновляется только первый TextView, а второй TextView продолжает показывать текущую дату.

Вот код:

package com.datepicker;

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.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class datepicker extends Activity {

private TextView mDateDisplay;
private TextView endDateDisplay;
private Button mPickDate;
private Button endPickDate;
private int mYear;
private int mMonth;
private int mDay;

static final int START_DATE_DIALOG_ID = 0;
static final int END_DATE_DIALOG_ID = 0;


/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /*  capture our View elements for the start date function   */
    mDateDisplay = (TextView) findViewById(R.id.startdateDisplay);
    mPickDate = (Button) findViewById(R.id.startpickDate);

    /* add a click listener to the button   */
    mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(START_DATE_DIALOG_ID);
        }
    });

    /* get the current date */
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    /* display the current date (this method is below)  */
    updateStartDisplay();


 /* capture our View elements for the end date function */
    endDateDisplay = (TextView) findViewById(R.id.enddateDisplay);
    endPickDate = (Button) findViewById(R.id.endpickDate);

    /* add a click listener to the button   */
    endPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(END_DATE_DIALOG_ID);
        }
    });

    /* get the current date */
    final Calendar c1 = Calendar.getInstance();
    mYear = c1.get(Calendar.YEAR);
    mMonth = c1.get(Calendar.MONTH);
    mDay = c1.get(Calendar.DAY_OF_MONTH);

    /* display the current date (this method is below)  */
    updateEndDisplay();
}



private void updateEndDisplay() {
    endDateDisplay.setText(
            new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-")
                .append(mDay).append("-")
                .append(mYear).append(" "));

}



private void updateStartDisplay() {
    mDateDisplay.setText(
            new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-")
                .append(mDay).append("-")
                .append(mYear).append(" "));


}

/* обратный вызов, полученный, когда пользователь "устанавливает" дату в диалоговом окне для функции даты начала */

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

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateStartDisplay();
            }
        };

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case START_DATE_DIALOG_ID:
        return new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
    }
    return null;
}
/* the callback received when the user "sets" the date in the dialog for the end date function  */

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

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateStartDisplay();
            }
        };

protected Dialog onCreateDialog1(int id) {
    switch (id) {
    case END_DATE_DIALOG_ID:
        return new DatePickerDialog(this,
                    endDateSetListener,
                    mYear, mMonth, mDay);
    }
    return null;
}

}

Пожалуйста, сообщите об изменениях, необходимых для кода.

4b9b3361

Ответ 1

Вам нужно сделать 2 отдельных диалоговых окна DatePicker

Сделайте 2 прослушивателя

int from_year, from_month, from_day,to_year, to_month, to_day; //initialize them to current date in onStart()/onCreate()
DatePickerDialog.OnDateSetListener from_dateListener,to_dateListener;

Внедрить их...

         from_dateListener = new OnDateSetListener(){

            public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {

                          ...

                }
            }

        };
        to_dateListener = new OnDateSetListener(){
            public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
                .....
            }
        };

Создайте отдельные диалоги для обоих из них

int DATE_PICKER_TO = 0;
int DATE_PICKER_FROM = 1;

@Override
protected Dialog onCreateDialog(int id) {

    switch(id){
        case DATE_PICKER_FROM:
                return new DatePickerDialog(this, from_dateListener, from_year, from_month, from_day); 
            case DATE_PICKER_TO:
                return new DatePickerDialog(this, to_dateListener, to_year, to_month, to_day);
    }
        return null;
}

Ответ 2

У меня есть решение, которое позволяет неограниченное количество полей даты без добавления новых типов диалоговых окон. Когда пользователь нажимает на одну из кнопок, я регистрирую, какие TextView и Календарь в настоящее время изменяются до запуска DatePickerDialog. Диалог OnDateSetListener затем обновляет зарегистрированный TextView и Календарь.

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class MultiDatePickerActivity extends Activity {

    private TextView startDateDisplay;
    private TextView endDateDisplay;
    private Button startPickDate;
    private Button endPickDate;
    private Calendar startDate;
    private Calendar endDate;

    static final int DATE_DIALOG_ID = 0;

    private TextView activeDateDisplay;
    private Calendar activeDate;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.multidatepicker);

        /*  capture our View elements for the start date function   */
        startDateDisplay = (TextView) findViewById(R.id.startDateDisplay);
        startPickDate = (Button) findViewById(R.id.startPickDate);

        /* get the current date */
        startDate = Calendar.getInstance();

        /* add a click listener to the button   */
        startPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDateDialog(startDateDisplay, startDate);
            }
        });

        /* capture our View elements for the end date function */
        endDateDisplay = (TextView) findViewById(R.id.endDateDisplay);
        endPickDate = (Button) findViewById(R.id.endPickDate);

        /* get the current date */
        endDate = Calendar.getInstance();

        /* add a click listener to the button   */
        endPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDateDialog(endDateDisplay, endDate);
            }
        });

        /* display the current date (this method is below)  */
        updateDisplay(startDateDisplay, startDate);
        updateDisplay(endDateDisplay, endDate);
    }

    private void updateDisplay(TextView dateDisplay, Calendar date) {
        dateDisplay.setText(
                new StringBuilder()
                    // Month is 0 based so add 1
                    .append(date.get(Calendar.MONTH) + 1).append("-")
                    .append(date.get(Calendar.DAY_OF_MONTH)).append("-")
                    .append(date.get(Calendar.YEAR)).append(" "));

    }

    public void showDateDialog(TextView dateDisplay, Calendar date) {
        activeDateDisplay = dateDisplay;
        activeDate = date;
        showDialog(DATE_DIALOG_ID);
    }

    private OnDateSetListener dateSetListener = new OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            activeDate.set(Calendar.YEAR, year);
            activeDate.set(Calendar.MONTH, monthOfYear);
            activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateDisplay(activeDateDisplay, activeDate);
            unregisterDateDisplay();
        }
    };

    private void unregisterDateDisplay() {
        activeDateDisplay = null;
        activeDate = null;
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
        }
        return null;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);
        switch (id) {
            case DATE_DIALOG_ID:
                ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
                break;
        }
    }
}

Такая гибкость полезна в приложении, где вы не знаете, сколько наборов данных вам понадобится до выполнения.

Ответ 3

Развернув вариант Адама в немного более легкой интерпретации веса и потенциально более полезный, я решил сохранить ссылку на int для идентификатора элемента, который создавал запрос диалога, а затем просто ссылался на это в обработчике конечных событий. Это имеет дополнительное преимущество в том, чтобы прекрасно вписываться в оператор switch в этом методе, если у вас есть несколько входов даты, но требуется определенное форматирование для каждого или каждого из них. Все фрагменты ниже приведены в моем классе активности напрямую

Переменные экземпляра

private static final int        DIALOG_DATE_PICKER  = 100;
private int                     datePickerInput;

Обработчик диалоговых окон

@Override
public Dialog onCreateDialog(int id)
{
    switch(id) {
        case DIALOG_DATE_PICKER:
            final Calendar c = Calendar.getInstance();
            DatePickerDialog dialog = new DatePickerDialog(this, dateSetListener, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
            return dialog;
    }
    return null;
}

Нажмите "Слушатель"

private OnClickListener datePickerListener =
    new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        datePickerInput = v.getId();
        showDialog(DIALOG_DATE_PICKER);
    }
};

Обработчик выбора даты

private DatePickerDialog.OnDateSetListener dateSetListener =
    new DatePickerDialog.OnDateSetListener()
{
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
    {
        switch( datePickerInput ) {
            case R.id.datePicker1:
                ((EditText) findViewById( datePickerInput ))
                    .setText(...);
                ...
                break;
            case R.id.datePicker2:
                ...
                break;
            default:
                ...
                break;
        }
    }
};

Ответ 4

Думаю, я нашел более чистое решение. Это сочетание того, что рекомендовано Google, и комментариев, которые я читаю здесь. В моем случае он даже работает при вызове из фрагмента Viewpager. В принципе, я передаю набор аргументов фрагменту диалога при вызове диалога выбора из моего фрагмента, как определено здесь: Android: передача данных (дополнительных) в фрагмент Затем я возвращаю значение пакета в свой класс DialogFragment и включаю его значение.

Вот два слушателя обеих кнопок startDate и endDate, из моего кода фрагмента:

    mWylStartDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Bundle bundle = new Bundle();
            bundle.putInt("DATE",1);

            DialogFragment newFragment = new DatePickerFragment();
            newFragment.setArguments(bundle);

            newFragment.show(getFragmentManager(), "datePicker");
        }
    });

    mWylEndDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Bundle bundle = new Bundle();
            bundle.putInt("DATE",2);

            DialogFragment newFragment = new DatePickerFragment();
            newFragment.setArguments(bundle);

            newFragment.show(getFragmentManager(), "datePicker");

        }
    });

Здесь мой класс DatePickerFragment

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

static final int START_DATE = 1;
static final int END_DATE = 2;

private int mChosenDate;

int cur = 0;


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);


    Bundle bundle = this.getArguments();
    if(bundle != null){
        mChosenDate = bundle.getInt("DATE",1);
    }



    switch (mChosenDate) {

        case START_DATE:
            cur = START_DATE;
            return new DatePickerDialog(getActivity(), this, year, month, day);

        case END_DATE:
            cur = END_DATE;
            return new DatePickerDialog(getActivity(), this, year, month, day);

    }
    return null;
}


@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {

    if(cur == START_DATE){
        // set selected date into textview
        Log.v("Date Début","Date1 : " + new StringBuilder().append(month + 1)
                .append("-").append(day).append("-").append(year)
                .append(" "));
    }
    else{
        Log.v("Date fin","Date2 : " + new StringBuilder().append(month + 1)
                .append("-").append(day).append("-").append(year)
                .append(" "));
    }
}

}

Ответ 5

вы можете просто использовать этот тип

public class MainActivity extends Activity {

     private TextView startDateDisplay;
        private TextView endDateDisplay;
        private Button startPickDate;
        private Button endPickDate;
        private Calendar startDate;
        private Calendar endDate;

        static final int DATE_DIALOG_ID = 0;

        private TextView activeDateDisplay;
        private Calendar activeDate;

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

            /*  capture our View elements for the start date function   */
            startDateDisplay = (TextView) findViewById(R.id.startDateDisplay);
            startPickDate = (Button) findViewById(R.id.startPickDate);

            /* get the current date */
            startDate = Calendar.getInstance();

            /* add a click listener to the button   */
            startPickDate.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    showDateDialog(startDateDisplay, startDate);
                }
            });

            /* capture our View elements for the end date function */
            endDateDisplay = (TextView) findViewById(R.id.endDateDisplay);
            endPickDate = (Button) findViewById(R.id.endPickDate);

            /* get the current date */
            endDate = Calendar.getInstance();

            /* add a click listener to the button   */
            endPickDate.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    showDateDialog(endDateDisplay, endDate);
                }
            });

            /* display the current date (this method is below)  */
            updateDisplay(startDateDisplay, startDate);
            updateDisplay(endDateDisplay, endDate);
        }

        private void updateDisplay(TextView dateDisplay, Calendar date) {
            dateDisplay.setText(
                    new StringBuilder()
                        // Month is 0 based so add 1
                        .append(date.get(Calendar.MONTH) + 1).append("-")
                        .append(date.get(Calendar.DAY_OF_MONTH)).append("-")
                        .append(date.get(Calendar.YEAR)).append(" "));

        }

        public void showDateDialog(TextView dateDisplay, Calendar date) {
            activeDateDisplay = dateDisplay;
            activeDate = date;
            showDialog(DATE_DIALOG_ID);
        }

        private OnDateSetListener dateSetListener = new OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                activeDate.set(Calendar.YEAR, year);
                activeDate.set(Calendar.MONTH, monthOfYear);
                activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateDisplay(activeDateDisplay, activeDate);
                unregisterDateDisplay();
            }
        };

        private void unregisterDateDisplay() {
            activeDateDisplay = null;
            activeDate = null;
        }

        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
                case DATE_DIALOG_ID:
                    return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
            }
            return null;
        }

        @Override
        protected void onPrepareDialog(int id, Dialog dialog) {
            super.onPrepareDialog(id, dialog);
            switch (id) {
                case DATE_DIALOG_ID:
                    ((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DAY_OF_MONTH));
                    break;
            }
        }


}

Ответ 6

просто сделайте логическую переменную в mainActivity следующим образом:

private boolean startDateOrEndDAte = true;

затем сделайте так, чтобы слушатели кнопок вот так, и измените значение переменной для кнопки evrey:

DialogFragment datePicker = new DatePickerFragment();

attendDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePicker.show(getSupportFragmentManager(),"Date Picker");
                startOrEnd = true ;
            }
        });
        leaveDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datePicker.show(getSupportFragmentManager(),"Date Picker");
                startOrEnd = false ;
            }
        });

тогда в метод onDateSet просто добавьте, если это утверждение ::

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



    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    String date = DateFormat.getDateInstance().format(c.getTime());
    attendDate = findViewById(R.id.attend_date);
    leaveDate = findViewById(R.id.leave_date);

    if (startOrEnd) {
        attendDate.setText(date);
    } else {
        leaveDate.setText(date);
    }
}

Ответ 7

Меня не устраивало какое-либо из вышеупомянутых решений, поэтому я сделал свой собственный: https://gist.github.com/JoachimR/f82b2b371b1ced4a09918c970e045d4f

import android.app.DatePickerDialog
import android.app.Dialog
import android.os.Bundle
import android.support.v4.app.DialogFragment
import java.util.*


class ChooseDayDialog : DialogFragment() {

    companion object {

        private val KEY_INITIAL_TIMESTAMP = "KEY_INITIAL_TIMESTAMP"
        private val KEY_REQUEST_CODE = "KEY_REQUEST_CODE"

        private val DEFAULT_REQUEST_CODE = 20

        fun createInstance(initialTimestamp: Long,
                           requestCode: Int = DEFAULT_REQUEST_CODE) =
                ChooseDayDialog().apply {
                    arguments = Bundle().apply {
                        putLong(KEY_INITIAL_TIMESTAMP, initialTimestamp)
                        putInt(KEY_REQUEST_CODE, requestCode)
                    }
                }

    }

    interface OnDayChosenListener {

        fun onDayChosen(requestCode: Int, year: Int, month: Int, dayOfMonth: Int)

    }

    private lateinit var listener: OnDayChosenListener

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val activity = activity
        if (activity is OnDayChosenListener) {
            listener = activity
        } else {
            throw IllegalStateException("Activity must implement OnDayChosenListener")
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val timestamp = arguments?.getLong(KEY_INITIAL_TIMESTAMP, -1L) ?: -1L
        if (timestamp == -1L) {
            throw IllegalStateException("no initial time given")
        }
        val requestCode = arguments?.getInt(KEY_REQUEST_CODE, DEFAULT_REQUEST_CODE)
                ?: DEFAULT_REQUEST_CODE

        val calendar = Calendar.getInstance().apply {
            timeInMillis = timestamp
        }
        return DatePickerDialog(activity,
                DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
                    listener.onDayChosen(requestCode, year, month, dayOfMonth)
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH))
    }

}

Ответ 8

вы можете просто использовать следующий код

@Override
protected Dialog onCreateDialog(int id) {
    /*
     * final Dialog d = new Dialog(this); d.set
     */

    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this,
                android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                mDateSetListener, cmYear, cmMonth, cmDay);

    }
    return null;
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        cmYear = year;
        cmMonth = monthOfYear;
        cmDay = dayOfMonth;
        updateDisplay();
    }
};

private void updateDisplay() {

    String date_check = ""
            + new StringBuilder()
                    // Month is 0 based so add 1
                    .append(cmYear).append("-").append(cmMonth + 1)
                    .append("-").append(cmDay).append(" ");
}

вы можете вызвать Dialog в любом onclick

showDialog(DATE_DIALOG_ID);

где DATE_DIALOG_ID объявляется как

static final int DATE_DIALOG_ID = 0;

Надеюсь, это полезно.

Ответ 9

попробуйте этот код. Кнопки "От" и "Кому" при нажатии показывают другой диалог выбора даты, и выбранная дата затем используется для установки видов текста

   From.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // new DatePickerDialog(TripPlanner.this, from_dateListener, from_year, from_month, from_day).show();


  new DatePickerDialog(TripPlanner.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    //DO SOMETHING

                    Fromdate.setText(String.valueOf(dayOfMonth)+"/"+String.valueOf(monthOfYear+1)+"/"+String.valueOf(year));

                }
            }, 2015, 02, 26).show();


        }
    });




  To.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {




        new DatePickerDialog(TripPlanner.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                //DO SOMETHING

                Todate.setText(String.valueOf(dayOfMonth)+"/"+String.valueOf(monthOfYear+1)+"/"+String.valueOf(year));





            }
        }, 2015, 02, 26).show();





    }
});

Ответ 10

    issue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Bundle bundle = new Bundle();
            bundle.putInt("DATE", 1);

            DialogFragment newFragment = new MyDatePickerFragment();
            newFragment.setArguments(bundle);
            newFragment.show(getSupportFragmentManager(), "datePicker");
        }
    });

    expiry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Bundle bundle = new Bundle();
            bundle.putInt("DATE", 2);

            DialogFragment newFragment = new MyDatePickerFragment();
            newFragment.setArguments(bundle);
            newFragment.show(getSupportFragmentManager(), "datePicker");

        }
    });

}


@Override
public void sendInput(String input, int id) {
    date = input;
    switch (id) {
        case 1:
            issue.setText(date);
            break;
        case 2:
            expiry.setText(date);
            break;
    }




public class MyDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    static final int ISSUE_DATE = 1;
    static final int EXPIRY_DATE = 2;

    private int mChosenDate;

    int cur = 0;


    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {

        String date;
        if (cur == ISSUE_DATE) {
            // set selected date into textview

            Log.v("Date Issue", "Date1 : " + new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));
            date = day + "/0" + (month + 1) + "/" + year;
        } else {
            Log.v("Date expiry", "Date2 : " + new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));
            date = day + "/0" + (month + 1) + "/" + year;

        }
        listenerforActivity.sendInput(date, cur);
    }

    public interface OnDateSetListenerInterface {
        void sendInput(String input, int id);
    }

    public OnDateSetListenerInterface listenerforActivity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            listenerforActivity = (OnDateSetListenerInterface) getActivity();
        } catch (ClassCastException e) {
            Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);


        Bundle bundle = this.getArguments();
        if (bundle != null) {
            mChosenDate = bundle.getInt("DATE", 1);
        }


        switch (mChosenDate) {

            case ISSUE_DATE:
                cur = ISSUE_DATE;
                return new DatePickerDialog(getActivity(), this, year, month, day);

            case EXPIRY_DATE:
                cur = EXPIRY_DATE;
                return new DatePickerDialog(getActivity(), this, year, month, day);

        }
        return null;
    }


}

Ответ 11

Мое решение для этого было просто с помощью переключателя

public class RegisterActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {
private TextView mDisplayBirthDay;
private TextView mDisplayExpDate;
private TextView mDisplayIssueDate;
private int sw;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false); // to get rid of the title of the activity
    Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

    mDisplayBirthDay = findViewById(R.id.birthDate);
    findViewById(R.id.birthDate).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDatePickerDialog();
            sw = 0;
        }
    });
    mDisplayExpDate = findViewById(R.id.editText_expire);
    findViewById(R.id.editText_expire).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDatePickerDialog();
            sw = 1;
        }
    });
    mDisplayIssueDate = findViewById(R.id.editText_issueDate);
    findViewById(R.id.editText_issueDate).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDatePickerDialog();
            sw = 2;
        }
    });
}
private  void showDatePickerDialog(){
    DatePickerDialog datePickerDialog = new DatePickerDialog(this,
            this,
            Calendar.getInstance().get(Calendar.YEAR),
            Calendar.getInstance().get(Calendar.MONTH),
            Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
    datePickerDialog.show();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.show_registrations:
            getSupportFragmentManager().beginTransaction().replace(R.id.event_description,
                    //TODO: pass eventID as intend when clicked on event
                    EventRegistration.newInstance(69)).commit();
            break;
        case R.id.visibility_event:

            break;
        case android.R.id.home:
            onBackPressed(); //handling the "back" button
            break;
        default:
    }
    return true;
}

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    month = month + 1;  //because January is the month 0
    String date = dayOfMonth + "/" + month + "/" + year;
    switch (sw){
        case 0:
            mDisplayBirthDay.setText(date);
            break;
        case 1:
            mDisplayExpDate.setText(date);
            break;
        case 2:
            mDisplayIssueDate.setText(date);
            break;
        default:
    }

}

}

Ответ 12

Вы можете просто использовать переменную флага boolean для определения вида, с которого вы звоните.