简体   繁体   中英

How to have multiple datepickers on one Android screen?

I created one screen in my Android app which contains a datepicker like so:

    TextView userDateView = (TextView) getView().findViewById(R.id.text_user_date);
    userDateView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DatePickerFragment newFragment = new DatePickerFragment();
            newFragment.setListner(OverviewFragment.this);
            newFragment.show(getFragmentManager(), "datePicker");
        }
    });

and I created a callback which receives the date back and updates the userDateView, which all works fine. In another screen I now want to have two dates next to eachother which the user can both select. Unfortunately the callback doesn't know from which of the two dates the user started the datepicker fragment.

Does anybody know how I can somehow know from which button the datepickerFragment was started? All tips are welcome!

A simple approach is to supply a parameter to each DatePickerFragment and send this parameter back to your callback, like requestCode on Activity.onActivityResult .

See how to supply parameters to Fragments: http://developer.android.com/reference/android/app/Fragment.html#setArguments(android.os.Bundle)

Btw, you can find a different approach here:

Multiple DatePickers in same activity

Try something like this:

DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setListner(new DatePickerDialog.OnDateSetListener() {
    @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)    {}
    });
newFragment.show(getFragmentManager(), "datePicker");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM