Android Lollipop中的自定义日期选择器对话框

时间:2014-10-20 07:43:48

标签: android datepicker android-5.0-lollipop custom-datetimepicker

我希望日期选择器只显示月份和年份。我已经定制了日期选择器,即从选择器中删除“day”字段,但在Android Lollipop Am中获取日,月和年的选择器。以下是我的一段代码。请帮我知道这个问题。提前谢谢。

    try {
        Field f[] = mDatePicker.getClass().getDeclaredFields();
        for (Field field : f) {

            if (field.getName().equals("mDaySpinner") || field.getName().equals("mDayPicker")) {
                field.setAccessible(true);
                Object dayPicker = new Object();
                dayPicker = field.get(mDatePicker);
                ((View) dayPicker).setVisibility(View.GONE);
            }

        }
    } catch (SecurityException e) {
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    }

3 个答案:

答案 0 :(得分:10)

使用反射来查找和隐藏UI元素并不是一个很好的做法。在您的情况下,它停止在棒棒糖中工作,因为mDaySpinner现在包含在DatePickerSpinngerDelegate类中的内部私有静态DatePicker类中。

我建议通过视图层次结构来查找和隐藏日期微调器元素。我编写了以下在棒棒糖中运行的代码:

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
    if (daySpinnerId != 0) {
        View daySpinner = datePicker.findViewById(daySpinnerId);
        if (daySpinner != null) {
            daySpinner.setVisibility(View.GONE);
        }
    }
}

答案 1 :(得分:0)

这是一个月份选择器的基本示例,但也可以很容易地适应年份和/或日期(也适用于较旧的Android版本):

public void initMonthPicker(){
dp_mes = (DatePicker) findViewById(R.id.dp_mes);

int year    = dp_mes.getYear();
int month   = dp_mes.getMonth();
int day     = dp_mes.getDayOfMonth();

dp_mes.init(year, month, day, new DatePicker.OnDateChangedListener() {
    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        month_i = monthOfYear + 1;
        Log.e("selected month:", Integer.toString(month_i));
     //Add whatever you need to handle Date changes
    }
});

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
    int daySpinnerId = Resources.getSystem().getIdentifier("day", "id", "android");
    if (daySpinnerId != 0)
    {
        View daySpinner = dp_mes.findViewById(daySpinnerId);
        if (daySpinner != null)
        {
            daySpinner.setVisibility(View.GONE);
        }
    }

    int monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
    if (monthSpinnerId != 0)
    {
        View monthSpinner = dp_mes.findViewById(monthSpinnerId);
        if (monthSpinner != null)
        {
            monthSpinner.setVisibility(View.VISIBLE);
        }
    }

    int yearSpinnerId = Resources.getSystem().getIdentifier("year", "id", "android");
    if (yearSpinnerId != 0)
    {
        View yearSpinner = dp_mes.findViewById(yearSpinnerId);
        if (yearSpinner != null)
        {
            yearSpinner.setVisibility(View.GONE);
        }
    }
} else { //Older SDK versions
    Field f[] = dp_mes.getClass().getDeclaredFields();
    for (Field field : f)
    {
        if(field.getName().equals("mDayPicker") || field.getName().equals("mDaySpinner"))
        {
            field.setAccessible(true);
            Object dayPicker = null;
            try {
                dayPicker = field.get(dp_mes);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            ((View) dayPicker).setVisibility(View.GONE);
        }

        if(field.getName().equals("mMonthPicker") || field.getName().equals("mMonthSpinner"))
        {
            field.setAccessible(true);
            Object monthPicker = null;
            try {
                monthPicker = field.get(dp_mes);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            ((View) monthPicker).setVisibility(View.VISIBLE);
        }

        if(field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner"))
        {
            field.setAccessible(true);
            Object yearPicker = null;
            try {
                yearPicker = field.get(dp_mes);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            ((View) yearPicker).setVisibility(View.GONE);
        }
    }
}

}

答案 2 :(得分:-1)

提前做出一个名为Simple Date picker的修改日期选择器...使用类似于日期选择器的代码来显示月份和年份

请参阅https://github.com/resengupta/Month-Year-Date-Picker

SimpleDatePickerDialog.java类负责显示月份和年份号码选择器。 SimpleDatePickerDelegate.java用于将规则应用于数字选择器。 SimpleDatePickerDialogFragment.java是一个包装警报对话框的DialogFragment。