在布局xml文件中,添加以下代码:
在activity类中添加以下代码:
public void showDatePickerDialog(View view) { DatePickerFragment fragment = new DatePickerFragment(); fragment.show(getSupportFragmentManager(), "datePicker"); }
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String date = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
EditText editText_date = (EditText) getActivity().findViewById(R.id.editText_date);
editText_date.setText(date);
}
}
然后日期选择器就能正常使用了。
上一篇:AndroidStudio中Retrofit返回的boolean值应为true,但实际上为false。
下一篇:AndroidStudio中Robolectric测试中ActivityBundle对象为null,是否测试过早?