Android 在 Fragment 中实现 DatePicker
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20923604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing DatePicker in Fragment
提问by AndroidNewbie
I'am using Fragment which is an instance of Fragment and not of DialogFragment.
我正在使用 Fragment,它是 Fragment 的一个实例,而不是 DialogFragment 的实例。
I did google most of the search result shows how to use DialogFragment to have DatePicker.
我确实用谷歌搜索了大部分搜索结果,显示了如何使用 DialogFragment 来拥有 DatePicker。
which isn't working in my case because of type mismatch of Fragment and DialogFragment
由于 Fragment 和 DialogFragment 的类型不匹配,这在我的情况下不起作用
Any example or idea would be helpful
任何示例或想法都会有所帮助
Here is the Fragment Java Code
这是片段 Java 代码
public class CreateReportFragment extends Fragment {
public CreateReportFragment(){}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
rootView = inflater.inflate(R.layout.activity_create_report, container, false);
initViews();
return rootView;
}
private void initViews()
{
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
editTextDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(year)
.append("-")
.append(month + 1)
.append("-").append(day));
buttonDate = (Button)rootView.findViewById(R.id.buttonDate);
}
How to implement DatePickerin Fragment?
如何在Fragment 中实现DatePicker?
回答by Raghunandan
Use a DialogFragment
用一个 DialogFragment
If i am guessing right you want to show a DatePicker on click of buttonDate
如果我猜对了,您想在单击 buttonDate 时显示 DatePicker
http://developer.android.com/guide/topics/ui/controls/pickers.html
http://developer.android.com/guide/topics/ui/controls/pickers.html
On Button click
在按钮上单击
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");
DatePickerFragment.java
日期选择器片段.java
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
}
}
回答by CommonsWare
How to implement DatePicker in Fragment?
如何在 Fragment 中实现 DatePicker?
The same way you would "implement" a TextView
in a Fragment
: have onCreateView()
return a View
that is or contains a DatePicker
. For example, you might have a layout file that contains a DatePicker
widget, and have onCreateView()
inflate that layout.
与您TextView
在 a 中“实现” a 的方式相同Fragment
:onCreateView()
返回 aView
是或包含 a DatePicker
。例如,您可能有一个包含DatePicker
小部件的布局文件,并且onCreateView()
对该布局进行了膨胀。
回答by Ojonugwa Jude Ochalifu
. . . which isn't working in my case because of type mismatch of Fragment and DialogFragment
. . . 由于 Fragment 和 DialogFragment 的类型不匹配,这在我的情况下不起作用
A DialogFragment
IS-A Fragment
because it extends Fragment.Therefore, you can use a DialogFragment anywhere you would a Fragment.
A DialogFragment
IS-AFragment
因为它扩展了 Fragment。因此,您可以在任何需要使用 Fragment 的地方使用 DialogFragment。