如何使用日期选择器对话框在datepicker中仅显示月份和年份

时间:2012-06-29 11:27:13

标签: android

请告诉我如何使用日期选择器对话框

在Datepicker中仅显示月份和年份
private DatePickerDialog.OnDateSetListener mDateSetListener2 =   new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, 
                              int monthOfYear, int dayOfMonth) {
            try {
                Log.e("","myear"+mYear_1+","+"myear"+mMonth_1+","+mDay_1);
             Field f[] = mDateSetListener2.getClass().getDeclaredFields();
             for (Field field : f) {
                 if (field.getName().equals("mDatePicker")) {
                     field.setAccessible(true);
                  Object dayPicker = new Object();
                  dayPicker = field.get(mDateSetListener2);
                  ((View) dayPicker).setVisibility(View.GONE);
                 }
             }
                     } catch (SecurityException e) {
             Log.e("ERROR", e.getMessage());
             } 
             catch (IllegalArgumentException e) {
             Log.e("ERROR", e.getMessage());
             } catch (IllegalAccessException e) {
             Log.e("ERROR", e.getMessage());
             }

            }
    };

4 个答案:

答案 0 :(得分:1)

AFAIK如果你想实现它,你可能必须自定义。

但幸运的是,有很好的DateSlider Framework Here

哪个应该给你以下输出。

enter image description here

代码托管在

http://code.google.com/p/android-dateslider/

他们也转移到Git,你可以找到最新的代码

https://github.com/bendemboski/DateSlider

答案 1 :(得分:1)

picker = (DatePicker) findViewById(R.id.datePicker1);
try {
    Field f[] = picker.getClass().getDeclaredFields();
    for (Field field : f) {
        if (field.getName().equals("mDayPicker")) {
            field.setAccessible(true);
            Object dayPicker = new Object();
            dayPicker = field.get(picker);
            ((View) dayPicker).setVisibility(View.GONE);
        }
    }
} catch (SecurityException e) {
   Log.d("ERROR", e.getMessage());
} catch (IllegalArgumentException e) {
   Log.d("ERROR", e.getMessage());
} catch (IllegalAccessException e) {
   Log.d("ERROR", e.getMessage());
}

答案 2 :(得分:1)

回答ρяσѕρєя如果我们更换K,K的效果非常好:

  

((ViewGroup)dp.getChildAt(1))。getChildAt(1).setVisibility(View.GONE);

通过

  

((ViewGroup中)((的ViewGroup)dp.getChildAt(0))getChildAt(0)。)getChildAt(0).setVisibility(View.GONE);

答案 3 :(得分:0)

只有通过创建自定义DatePickerDialog才能实现:

第1步:将DatePickerDialog扩展为:

class CustomerDatePickerDialog extends DatePickerDialog {

    public CustomerDatePickerDialog(Context context,
            OnDateSetListener callBack, int year, int monthOfYear,
            int dayOfMonth) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        super.onDateChanged(view, year, month, day);
        mDialog.setTitle((month + 1) + "-" + day + "-");
    }
}

第2步:

final Calendar cal = Calendar.getInstance();
        mDialog = new CustomerDatePickerDialog(getContext(), this,
            cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
            cal.get(Calendar.DAY_OF_MONTH));
        mDialog.show();

        DatePicker dp = findDatePicker((ViewGroup) mDialog.getWindow().getDecorView());
        if (dp != null) {
               //you can set setVisibility here for day,month and year here 0 for year
            ((ViewGroup) dp.getChildAt(1)).getChildAt(1).setVisibility(View.GONE);
        } 

  private DatePicker findDatePicker(ViewGroup group) {
        if (group != null) {
            for (int i = 0, j = group.getChildCount(); i < j; i++) {
                View child = group.getChildAt(i);
                if (child instanceof DatePicker) {
                    return (DatePicker) child;
                } else if (child instanceof ViewGroup) {
                    DatePicker result = findDatePicker((ViewGroup) child);
                    if (result != null)
                        return result;
                }
            }
        }
        return null;
    }