如何使DialogFragment和FragmentManager一个datepicker? [英] How to make a DatePicker with DialogFragment and FragmentManager?

查看:240
本文介绍了如何使DialogFragment和FragmentManager一个datepicker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在这一段时间,我似乎无法弄清楚。我在Android开发相当新的,所以忍耐一下吧。我是不是太熟悉创建一个datepicker,我学会了做去precated方式只是为了得到它的窍门。使用本教程让我加快速度:

So I have been at this for a while, and I cannot seem to figure it out. I am fairly new at Android development, so bear with me please. I wasn't too familiar with creating a Datepicker and I learned to do it the deprecated way just to get the hang of it. Used this tutorial to get me up to speed:

http://developer.android.com/resources/tutorials/意见/ HELLO-datepicker.html

但现在我需要去改变它,主要不是去precated code使用的,所以我环顾四周,我发现2教程,主要是这一个,但:

But now I need to change it, mainly to not use deprecated code, so I looked all around, and I found 2 tutorials, mainly this one though:

http://www.kylebeal.com/2011/11/android-datepickerdialog-and-the-dialogfragment/

问题是,有code,它是没有意义的。主要是这一部分:

Problem is, there is code that does not make sense. Mainly this part:

//create new DateDialogFragment
DateDialogFragment ddf = DateDialogFragment.newInstance(this, R.string.set_date, date);

ddf.setDateDialogFragmentListener(new DateDialogFragmentListener() {

    @Override
    public void dateDialogFragmentDateSet(Calendar date) {
        // update the fragment
        mDateDetailFragment.updateDate(date);
    }
});

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

这是假设是什么,它​​运行在活动课上运行,所以我有点弄什么整个code其余发生,并随时看看剩下的,因为它可能会帮助。在code基本上跟随几乎与原的ShowDialog() code相同的路线。实施 DatePickerDialog.onDateSetListener ,并返回一个新的 DatePickerDialog ,具有明显的异常使用DialogFragment其那。但是,这就是我所得到丢失。

That is suppose to be what is running in the activity class that it runs in. So I kind of get whats happening throughout the rest of the code, and feel free to look at the rest as it will probably help. The code is essentially following almost the same route as the original showDialog() code. Implementing DatePickerDialog.onDateSetListener, and returning a new DatePickerDialog, with the obvious exception thats its using a DialogFragment. But that is where I have gotten lost.

部分原因是因为我真的不知道的其中, onCreateDialog()返回新DatePickerDialog。如果你看一下code我说没有意义,既 mDateDetailFragment getSupportFragmentManager()从来没有实例化或出现其他地方,所以我不能让本教程code,甚至工作。 Android的文件还提到,而不是使用的ShowDialog()我要同时使用 DialogFragment FragmentManager ,也不能在这里完成。只有DialogFragment。

Part of the reason is because I am not really sure where onCreateDialog() returns that new DatePickerDialog. If you look at the code I said does not make sense, both mDateDetailFragment and getSupportFragmentManager() were never instantiated or appeared anywhere else, so I cannot get the tutorial code to even work. The Android Doc also mentions that instead of using showDialog() I should be using both DialogFragment and FragmentManager, which is also not done here. Only DialogFragment.

等等之类的标题,究竟如何可以使我在非depracted形式DatPicker?我想请要求不会送我一个链接,只说看这里。我已经做到了。相信我。如果没有,至少说明我已经错过了,使这个有意义。此外,如果任何人都可以真正告诉我哪里有 onCreateDialog()方法发送 DatePickerDialog ,这将是极其有益的井。我只是试图让这一切感觉,而不是复制和粘贴code(而不是它的工作原理反正)。非常感谢您抽出时间来阅读,如果有任何需要澄清,请询问。

So like the title, how exactly can I make a DatPicker in the non-depracted form? I would please ask to NOT send me to a link and just say look here. I have done that. Believe me. If not, at least explain what I have missed that makes this make sense. Also, if anyone can actually tell me where the onCreateDialog() method sends the DatePickerDialog, that would be extremely helpful as well. I am just trying to make sense of all this, rather than copy and paste code (not that it works anyways). Thanks a lot for taking the time out to read this, and if any clarifications needed, please ask.

推荐答案

在您的文章的顺序去... \\

Going in order of your post... \

1) onCreateDialog 返回对话框的DialogFragment。

1) the onCreateDialog returns the dialog to the DialogFragment.

2)我不知道有关 mDateDetailFragment.updateDate(日期); 行......我想他可能会留下一些code OUT。修改发现了它,它是在为他的完整演示zip文件中的一个类

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)你正在使用的片段管理器(它是 getSupportFragmentManager()部分):

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

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

好吧,这么说,这里是如何我已经做到了(我使用DateSlider,但它在一个datepicker底层的地方应该是足够简单你)。

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).

在片段/活性调用DialogFragment:

In the fragment/activity calling the DialogFragment:

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

和我DialogFragment:

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.

这篇关于如何使DialogFragment和FragmentManager一个datepicker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆