0

请告诉我如何使用 Datapicker 对话框在 Datapicker 中仅显示月份和年份

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

4 回答 4

1

AFAIK 如果您想实现它,您可能必须以自定义方式进行。

但幸运的是,这里有很好的 DateSlider 框架

哪个应该给你以下输出。

在此处输入图像描述

代码托管在

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

他们也搬到了 Git 上,你可以在其中找到最新的代码

https://github.com/bendemboski/DateSlider

于 2012-06-29T11:35:30.107 回答
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());
}
于 2013-03-23T14:05:54.273 回答
1

如果我们替换,ρяσѕρєя K 的回答可以完美地工作:

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

经过

((ViewGroup)((ViewGroup)dp.getChildAt(0)).getChildAt(0)).getChildAt(0).setVisibility(View.GONE);

于 2013-08-31T12:19:54.547 回答
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;
    } 
于 2012-06-29T11:59:20.787 回答