我正在试图找出如何在CalendarDatePicker上显示当前所选日期。 E.g.
如果我将2016年9月13日作为选定日期,则应在CalendarDatePicker上显示/突出显示我选择该日期的日期。所以我设置日期并将其显示在textview上,如果我更新日期,它应该在CalendarDatePicker上获得上一个选定的日期。我添加了他们的onDayOfMonthSelected()方法,但它仍然无法正常工作。这就是我到目前为止所拥有的。我正在使用来自github的这个库Android-BetterPicker。
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CalendarDatePickerDialogFragment cdp2 = new CalendarDatePickerDialogFragment()
.setOnDateSetListener(CalendarPicker.this);
cdp2.show(getSupportFragmentManager(), FRAG_TAG_DATE_PICKER);
cdp2.setOnDateSetListener(new CalendarDatePickerDialogFragment.OnDateSetListener() {
@Override
public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
mResultTextView.setText(getString(R.string.calendar_date_picker_result_values, year, monthOfYear+1, dayOfMonth));
}
});
}
});
这是我的onResume()函数
public void onResume() {
// Example of reattaching to the fragment
super.onResume();
CalendarDatePickerDialogFragment calendarDatePickerDialogFragment = (CalendarDatePickerDialogFragment) getSupportFragmentManager()
.findFragmentByTag(FRAG_TAG_DATE_PICKER);
if (calendarDatePickerDialogFragment != null) {
calendarDatePickerDialogFragment.onDayOfMonthSelected(CHOSEN_YEAR,CHOSEN_MONTH,CHOSEN_DAY);
calendarDatePickerDialogFragment.setOnDateSetListener(this);
}
}
答案 0 :(得分:1)
它们具有您想要的功能:
public void onResume() {
// Example of reattaching to the fragment
super.onResume();
CalendarDatePickerDialogFragment calendarDatePickerDialogFragment = (CalendarDatePickerDialogFragment) getSupportFragmentManager()
.findFragmentByTag(FRAG_TAG_DATE_PICKER);
if (calendarDatePickerDialogFragment != null) {
calendarDatePickerDialogFragment.onDayOfMonthSelected(CHOSEN_YEAR,CHOSEN_MONTH,CHOSEN_DAY);
calendarDatePickerDialogFragment.setOnDateSetListener(this);
}
}
也在这里改变:
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CalendarDatePickerDialogFragment cdp2 = new CalendarDatePickerDialogFragment()
.setOnDateSetListener(CalendarPicker.this);
cdp2.show(getSupportFragmentManager(), FRAG_TAG_DATE_PICKER);
//here
cdp2.onDayOfMonthSelected(CHOSEN_YEAR,CHOSEN_MONTH,CHOSEN_DAY);
cdp2.setOnDateSetListener(new CalendarDatePickerDialogFragment.OnDateSetListener() {
@Override
public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
mResultTextView.setText(getString(R.string.calendar_date_picker_result_values, year, monthOfYear+1, dayOfMonth));
}
});
}
});
答案 1 :(得分:0)
你可以这样:
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1;
int year = datePicker.getYear();
如果要在Button本身上显示当前日期,请使用以下日期:
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
MyDate mydate = new MyDate();
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (getArguments()!=null && getArguments().get("DATE") != null) {
mydate = (MyDate) getArguments().get("DATE");
}
DatePickerDialog dpd = new DatePickerDialog(getActivity(), this, mydate.year, mydate.month, mydate.day);
dpd.getDatePicker().setCalendarViewShown(false);
dpd.setTitle("Set Date");
return dpd;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
mydate = new MyDate(day,month,year);
if (getTag().equals("birthdatePicker")){
birthday.setText(mydate.toStringDate());
}
else if (getTag().equals("annidatePicker")){
anniversary.setText(mydate.toStringDate());
}
else if (getTag().equals("spousedatePicker")){
spousebday.setText(mydate.toStringDate());
}
}
public String getDate(){
return mydate.toString();
}
}
static class MyDate implements Parcelable {
int day;
int month;
int year;
public MyDate() {
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR)- 15;
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
}
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public String toString() {
return year + "-" + (month+1) + "-" + day;
}
public String toStringDate(){
return day + "-" + (month+1) + "-" + year;
}
public static MyDate toDate(String date) {
String [] strArray = date.split("/");
int d = Integer.parseInt(strArray[0]);
int m = Integer.parseInt(strArray[1])-1;
int y = Integer.parseInt(strArray[2]);
return new MyDate(d, m, y);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.day);
dest.writeInt(this.month);
dest.writeInt(this.year);
}
protected MyDate(Parcel in) {
this.day = in.readInt();
this.month = in.readInt();
this.year = in.readInt();
}
public static final Parcelable.Creator<MyDate> CREATOR = new Parcelable.Creator<MyDate>() {
@Override
public MyDate createFromParcel(Parcel source) {
return new MyDate(source);
}
@Override
public MyDate[] newArray(int size) {
return new MyDate[size];
}
};
}
public void showBirthDateDialog() {
birthFragment.show(getFragmentManager(), "birthdatePicker");
}
birthday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showBirthDateDialog();
}
});
我认为这有助于你的事业!