我使用了一个素材日期选择器,但它没有在日历视图中显示当前年份 Datepicker
from_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
flage_from=true;
flage_to=false;
datePickerDialog = DatePickerDialog.newInstance(MainActivity.this, Year, Month, Day);
datePickerDialog.setThemeDark(false);
datePickerDialog.showYearPickerFirst(false);
datePickerDialog.setAccentColor(Color.parseColor("#215f8b"));
// datePickerDialog.setTitle("Select Date");
datePickerDialog.show(getFragmentManager(), "DatePickerDialog");
}
});
这是我的代码
答案 0 :(得分:1)
我还添加了将所选日期设置为textview的代码。如果您不想要,可以删除该代码,如果需要,也可以更改格式
声明日历实例
Calendar c;
int year, month, day;
然后初始化
c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
按钮onClick Listener
bookdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get Current Date
DatePickerDialog dd = new DatePickerDialog(Wishlist_Detail_Activity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
try {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
Date date = formatter.parse(dateInString);
//bookdate.setText(formatter.format(date).toString());
formatter = new SimpleDateFormat("dd MMMM yyyy");
bookdate.setText(formatter.format(date).toString());
} catch (Exception ex) {
}
}
}, year, month, day);
dd.show();
}
});
答案 1 :(得分:0)
试试这个
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog =
new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
dialog.show();
答案 2 :(得分:0)
您可以尝试此解决方案:
final Calendar calendar = Calendar.getInstance();
final DatePicker datePicker = new DatePicker(this);
datePicker.setMinDate(calendar.getTimeInMillis());
datePicker.setCalendarViewShown(false);
new AlertDialog.Builder(this)
.setView(datePicker)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mActionsListener.filterEventsByDate(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
}
})
.show();
}
答案 3 :(得分:0)
试试这个
DatePickerDialog dpd = DatePickerDialog.newInstance(MainActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH);
dpd.show(getFragmentManager(), "Datepickerdialog");