我按照这里的说明:https://developer.android.com/guide/topics/ui/controls/pickers.html如何在Android Studio中正确创建日期选择器?
我向下滚动到“创建日期选择器”,并复制粘贴在一个文件中的代码名为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
类:
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
但是这是说getSupportFrammentManager()
没有定义?
我是使用Android工作室和开发Andriod的新手。我想要的是日期选择器(如果您访问上面提供的链接,它的图片会显示在屏幕的顶部附近)。任何想法如何解决这个问题?
你尝试Android的自身datepickerdialog https://developer.android.com/reference/android/app/DatePickerDialog.html –
您是否在活动中扩展Activity类或AppCompatActivity类? –