如何创建日期选择器和timePicker对话框中的片段类?片段、创建日期、选择器、对话框中

2023-09-12 03:19:37 作者:隐身守侯

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

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:

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(" "));

 }
 
精彩推荐
图片推荐