【问题标题】:Android Date selector with only Day and MonthAndroid 日期选择器,只有日和月
【发布时间】:2014-02-17 20:13:28
【问题描述】:

我正在寻找一种方法来自定义 Android 中的日期选择器以仅显示日期和月份(即没有年份)。

我正在创建一个基于How to display date picker for android with only month and year fields? 的对话框:

    Dialog dlg = new DatePickerDialog(context, datePickerListener, dueDateCalendar.get(Calendar.YEAR), dueDateCalendar.get(Calendar.MONTH), dueDateCalendar.get(Calendar.DAY_OF_MONTH));
    try {
        Field f[] = dlg.getClass().getDeclaredFields();
        for (Field field : f) {
            String name = field.getName();
            if (name.equals("YEAR")){
                field.setAccessible(true);
                Object dayPicker = new Object();
                dayPicker = field.get(dlg);
                ((View) dayPicker).setVisibility(View.GONE);
            }
        }
    } catch (Exception e){
        // TODO: should not happen
        e.printStackTrace();
    }
    return dlg;

但我在 ((View) dayPicker).setVisibility(View.GONE); 上不断收到 Cast 异常;

java.lang.ClassCastException: java.lang.String 无法转换为 android.view.View

有什么想法吗?

【问题讨论】:

    标签: android datepicker


    【解决方案1】:

    这里试一试。它的 APIv11+,但在较低的 API 版本上还有其他方法。

    DatePickerDialog dlg = new DatePickerDialog(context, datePickerListener, 
        dueDateCalendar.get(Calendar.YEAR), 
        dueDateCalendar.get(Calendar.MONTH), 
        dueDateCalendar.get(Calendar.DAY_OF_MONTH));
    int year = context.getResources().getIdentifier("android:id/year", null, null);
    if(year != 0){
        View yearPicker = dlg.getDatePicker().findViewById(year);
        if(yearPicker != null){
            yearPicker.setVisibility(View.GONE);
        }
    }
    return dlg;
    

    更新的代码:这应该可以完成工作。

    DatePickerDialog dlg = new DatePickerDialog(context, datePickerListener, 
        dueDateCalendar.get(Calendar.YEAR), 
        dueDateCalendar.get(Calendar.MONTH), 
        dueDateCalendar.get(Calendar.DAY_OF_MONTH))
    {
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            int year = getContext().getResources()
                .getIdentifier("android:id/year", null, null);
            if(year != 0){
                View yearPicker = findViewById(year);
                if(yearPicker != null){
                    yearPicker.setVisibility(View.GONE);
                }
            }
        }
    };
    return dlg;
    

    【讨论】:

    • 我把yearPicker改成了View yearPicker = dlg.findViewById(year);以避免 API 级别问题,但它符合但返回 null。有什么想法吗?
    • 在 Android 5.1 中找不到适合我的年份 :(
    • yearPicker 在 api 级别 23 上为空。
    • 在较新的 API 级别上,标题中 TextView 的年份 ID 为“android:id/date_picker_header_year”。
    【解决方案2】:

    这对我有用:

    DatePickerDialog monthDatePickerDialog = new DatePickerDialog(activity, 
    AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            monthTextView.setText(dayOfMonth+ "/" + (month + 1));
        }
    }, yearNow, monthNow, dayNow){
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getDatePicker().findViewById(getResources().getIdentifier("year","id","android")).setVisibility(View.GONE);
        }
    };
    monthDatePickerDialog.setTitle("select_month");
    monthDatePickerDialog.show();
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多