我按照此处的说明操作:https://developer.android.com/guide/topics/ui/controls/pickers.html
我向下滚动到“创建日期选择器”并将此代码复制粘贴到名为TargetPage
的文件中:
DisplayText
然后我在DatePickerFragment.java
:
public static 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);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
并将此代码添加到activity_front_page.xml
类:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pick_date"
android:onClick="showDatePickerDialog" />
但它是说DatePickerFramegent
没有定义?
我刚开始使用Android工作室并为Andriod开发。我想要的是日期选择器(如果您访问上面提供的链接,它的图片显示在屏幕顶部附近)。知道如何解决这个问题吗?
答案 0 :(得分:0)
当你使用android.support.v4.app.DialogFragment时,你应该传递给show()一个android.support.v4.app.FragmentManager的实例,可以使用getSupportFragmentManager()调用查询
答案 1 :(得分:0)
尝试以下代码,看看是否有帮助:
Activity activity = getActivity;
DatePickerDialog datePickerDialog = new DatePickerDialog(activity, AlertDialog.THEME_HOLO_LIGHT,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}, year, mm, dd);
datePickerDialog.show();
答案 2 :(得分:0)
您需要FragmentTransaction
而不是getSupportFragmentManager()
public void showDatePickerDialog(View v) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(ft, "datePicker");
}