我在android中使用日期选择器。我检查了许多github项目和问题,但找不到像图片那样的视图。我使用了许多代码但是徒劳无功。现在我正在使用此代码
public void showDatePicker() {
LayoutInflater inflater = (LayoutInflater) getLayoutInflater( bundle);
final AlertDialog.Builder dialogBuilder =
new AlertDialog.Builder(getActivity());
View customView = inflater.inflate(R.layout.current_project_fragment, null);
dialogBuilder.setView(customView);
final Calendar now = Calendar.getInstance();
final DatePicker datePicker =
(DatePicker) customView.findViewById(R.id.dialog_datepicker);
final TextView dateTextView =
(TextView) customView.findViewById(R.id.dialog_dateview);
final SimpleDateFormat dateViewFormatter =
new SimpleDateFormat("yyyy-MMM-dd",Locale.ENGLISH);
final SimpleDateFormat formatter =
new SimpleDateFormat("yyyy-MMM-dd",Locale.ENGLISH);
// Minimum date
Calendar minDate = Calendar.getInstance();
try {
minDate.setTime(formatter.parse("12.12.2010"));
} catch (ParseException e) {
e.printStackTrace();
}
datePicker.setMinDate(minDate.getTimeInMillis());
// View settings
dialogBuilder.setTitle("Choose a date");
Calendar choosenDate = Calendar.getInstance();
int year = choosenDate.get(Calendar.YEAR);
int month = choosenDate.get(Calendar.MONTH);
int day = choosenDate.get(Calendar.DAY_OF_MONTH);
try {
Date choosenDateFromUI = formatter.parse(
dateTextView.getText().toString()
);
choosenDate.setTime(choosenDateFromUI);
year = choosenDate.get(Calendar.YEAR);
month = choosenDate.get(Calendar.MONTH);
day = choosenDate.get(Calendar.DAY_OF_MONTH);
} catch (Exception e) {
e.printStackTrace();
}
Calendar dateToDisplay = Calendar.getInstance();
dateToDisplay.set(year, month, day);
dateTextView.setText(
dateViewFormatter.format(dateToDisplay.getTime())
);
// Buttons
dialogBuilder.setNegativeButton(
"Go to today",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dateTextView.setText(
formatter.format(now.getTime())
);
dialog.dismiss();
}
}
);
dialogBuilder.setPositiveButton(
"Choose",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Calendar choosen = Calendar.getInstance();
choosen.set(
datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth()
);
dateTextView.setText(
dateViewFormatter.format(choosen.getTime())
);
dialog.dismiss();
}
}
);
final AlertDialog dialog = dialogBuilder.create();
// Initialize datepicker in dialog atepicker
datePicker.init(
year,
month,
day,
new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar choosenDate = Calendar.getInstance();
choosenDate.set(year, monthOfYear, dayOfMonth);
dateTextView.setText(
dateViewFormatter.format(choosenDate.getTime())
);
if (choosenDate.get(Calendar.DAY_OF_WEEK) ==
Calendar.SUNDAY ||
now.compareTo(choosenDate) < 0) {
dateTextView.setTextColor(
Color.parseColor("#ff0000")
);
((Button) dialog.getButton(
AlertDialog.BUTTON_POSITIVE))
.setEnabled(false);
} else {
dateTextView.setTextColor(
Color.parseColor("#000000")
);
((Button) dialog.getButton(
AlertDialog.BUTTON_POSITIVE))
.setEnabled(true);
}
}
}
);
// Finish
dialog.show();
}
&#13;