How to make a DatePicker with DialogFragment and FragmentManager?

隐身守侯 提交于 2019-12-05 17:41:36

Going in order of your post... \

1) the onCreateDialog returns the dialog to the DialogFragment.

2) I don't know about the mDateDetailFragment.updateDate(date); line... I think he might have left some code out. EDIT Found it, it's in one of the classes in the zip file for his complete demo.

3) You are using the fragment manager (it's the getSupportFragmentManager() part):

ddf.show(getSupportFragmentManager(), "date picker dialog fragment");

Ok, that said, here's how I've done it (I'm using a DateSlider, but subbing in a Datepicker in it's place should be simple enough for you).

In the fragment/activity calling the DialogFragment:

DialogFragment newFragment = new DateDialog();
newFragment.show(getFragmentManager(), "dialog"); 

And my DialogFragment:

public class DateDialog extends DialogFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    private DateSlider.OnDateSetListener mDateSetListener = new DateSlider.OnDateSetListener() {
        public void onDateSet(DateSlider view, Calendar selectedDate) {
            MemberAddFragment.startTxt.setText(String.format("%tB %te, %tY", selectedDate, selectedDate, selectedDate));
        }
    };

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar c = Calendar.getInstance();  // Get calendar instance to use in DateSlider
        return new AlternativeDateSlider(getActivity(),mDateSetListener,c,null,null);  // return DateSlider dialog to DialogFragment
    }
}

Hope this helps clear up your confusion.

I know this question is mine and already has an answer. But I thought I'd add my own answer to others looking for help on this same issue. I have simplified the example code that I provided above into only 2 classes. Kbeal, who was gracious enough to provide the tutorial did a great job, but it was too bloated with information that was not even necessary. So using what I knew about dialogs and what I learned from what I could understand from the Kbeal's tutorial as well as the research I did on fragments, I have provided my implementation from a DatePickerDialog on my GitHub.

https://github.com/Zeroe31890/DateDialogFragment

All you honestly have to do is run it straight after only copy and pasting the 2 classes, the single layout, and modifying the Android Manifest file. Much easier than any of the other tutorials. Oh and if anybody sees anything that can be done better on it please do. Enjoy!

Author of the post here: Sounds like you have it all figured out. I replied to your questions on my post before finding this though Barak does a fine job of answering them. If you haven't done so already download the complete source for the example app. I think it will help fill any missing blanks you still have.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!