如何创建日期选择器和timePicker对话框中的片段类? [英] How to create datePicker and timePicker dialogs in fragment class?

查看:126
本文介绍了如何创建日期选择器和timePicker对话框中的片段类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有方法来创建一个片段的日期选择器?我创建一个定期活动可能,这让我的语法错误。什么是做到这一点的正确方法?

I want to know is there a way to create a datePicker in a fragment? I am creating one the regular activity may and it gives me syntax error. What is the correct way to do this?

推荐答案

您将最有可能需要使用DialogFragment。我发现了一些信息:

You will most likely need to use a DialogFragment. I found some information here:

从片段显示对话框?

和也是一个很大的帮助在这里:

and also a big help here:

<一个href="https://github.com/commonsguy/cw-advandroid/blob/master/Honeycomb/FeedFragments/src/com/commonsware/android/feedfrags/AddFeedDialogFragment.java" rel="nofollow">https://github.com/commonsguy/cw-advandroid/blob/master/Honeycomb/FeedFragments/src/com/commonsware/android/feedfrags/AddFeedDialogFragment.java

这应该可以帮助你得到你的方式,我做的这事了。虽然里面的例子code我不使用生成器,而是直接返回:

This should help you get on your way, I am doing this very thing now. Though inside the example code I don't use a builder and instead just return:

return new DatePickerDialog(getActivity(), mDateSetListener, mYear, mMonth, mDay);

这似乎工作...虽然我无法弄清楚如何尚未更新调用此DialogFragment碎片上的文字。我认为这会工作,它不会:

This seems to work... though I cannot figure out yet how to update the text on the fragment that calls this DialogFragment. I thought this would work and it doesn't:

 public void updateDisplay()
 {
     //update our button text from the calling fragment, this isn't quite working
     //doesn't crash just doesn't update...must be missing something.
     View v=getActivity()
        .getLayoutInflater()
        .inflate(R.layout.session_edits, null);
     Button sessionDate = (Button)v.findViewById(R.id.sessionPickDate);
            sessionDate.setText(new StringBuilder()
                            .append(mMonth+1).append("-").append(mDay).append("-").append(mYear).append(" "));

 }

这篇关于如何创建日期选择器和timePicker对话框中的片段类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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