目前我正在使用带有修改的原生对话框来隐藏日期Spinner并且工作正常。
但是现在我想以这样的格式设置日期:当我旋转月份微调器或年份微调器时,对话框只显示月份和年份而不是完整的日历日期我的日期选择器对话框的自定义代码如下:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
DatePickerDialog datePickerDialog = this.customDatePicker();
return datePickerDialog;
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker datepickerView, int selectedYear,
int selectedMonth, int selectedDay) {
/* year = selectedYear;
month = selectedMonth;
day = selectedDay;*/
mynewYear=selectedYear;
myNewMonth=selectedMonth;
myNewDay=selectedDay;
try {
checkDate();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*time_date_TV.setText(new StringBuilder()
.append(myNewMonth + 1).append("/")
.append(myNewDay ).append("/").append(mynewYear));
*/
}
};
private DatePickerDialog customDatePicker() {
DatePickerDialog dpd = new DatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog,datePickerListener,
year, month,day );
if(month==0)
{
mmonth="January";
}
if(month==1)
{
mmonth="Faburary";
}
if(month==2)
{
mmonth="March";
}
if(month==3)
{
mmonth="April";
}
if(month==4)
{
mmonth="May";
}
if(month==5)
{
mmonth="June";
}
if(month==6)
{
mmonth="July";
}
if(month==7)
{
mmonth="August";
}
if(month==8)
{
mmonth="September";
}if(month==9)
{
mmonth="October";
}
if(month==10)
{
mmonth="November";
}
if(month==11)
{
mmonth="December";
}
dpd.setTitle(""+mmonth+", "+year);
try {
Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
for (Field datePickerDialogField : datePickerDialogFields) {
if (datePickerDialogField.getName().equals("mDatePicker")) {
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField
.get(dpd);
Field datePickerFields[] = datePickerDialogField.getType()
.getDeclaredFields();
for (Field datePickerField : datePickerFields) {
if ("mDayPicker".equals(datePickerField.getName())
|| "mDaySpinner".equals(datePickerField
.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
}
}
} catch (Exception ex) {
}
return dpd;
}
当第一次弹出对话框时,它可以正常工作,因为我设置了
的标题 DatePickerDialog.setTitle("");
但是当第一次弹出对话框但是我想在ondatechangelistner
上更新时,这样可以正常工作,那么我该怎么做呢
答案 0 :(得分:2)
脑力激荡半天后,我找到了这个解决方案,这可能有助于其他有这种情况
我的日期选择器的自定义类是以下每次用户更改月/年时将更新标题中的日期
class CustomDatePickerDialog extends DatePickerDialog implements OnDateChangedListener {
private DatePickerDialog mDatePicker;
@SuppressLint("NewApi")
public CustomDatePickerDialog(Context context,int theme, OnDateSetListener callBack,
int year, int monthOfYear, int dayOfMonth) {
super(context, theme,callBack, year, monthOfYear, dayOfMonth);
mDatePicker = new DatePickerDialog(context,theme,callBack, year, monthOfYear, dayOfMonth);
mDatePicker.getDatePicker().init(2013, 7, 16, this);
updateTitle(year, monthOfYear);
}
public void onDateChanged(DatePicker view, int year,
int month, int day) {
updateTitle(year, month);
}
private void updateTitle(int year, int month) {
Calendar mCalendar = Calendar.getInstance();
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, month);
// mCalendar.set(Calendar.DAY_OF_MONTH, day);
mDatePicker.setTitle(getFormat().format(mCalendar.getTime()));
}
public DatePickerDialog getPicker(){
return this.mDatePicker;
}
/*
* the format for dialog tile,and you can override this method
*/
public SimpleDateFormat getFormat(){
return new SimpleDateFormat("MMM, yyyy");
};
}
以下是将日期选择器对话框
隐藏日期及其微调器的代码CustomDatePickerDialog dp = new CustomDatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog, datePickerListener, year, month, day);
DatePickerDialog obj = dp.getPicker();
try{
Field[] datePickerDialogFields = obj.getClass().getDeclaredFields();
for (Field datePickerDialogField : datePickerDialogFields) {
if (datePickerDialogField.getName().equals("mDatePicker")) {
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField.get(obj);
Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
for (Field datePickerField : datePickerFields) {
if ("mDayPicker".equals(datePickerField.getName()) || "mDaySpinner".equals(datePickerField
.getName())) {
datePickerField.setAccessible(true);
Object dayPicker = new Object();
dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
}
}
}
}
}catch(Exception ex){
}
obj.show();