Android只有材料设计的月份和年份选择器

时间:2015-09-18 10:54:24

标签: android material-design android-datepicker

我正在处理需要输入信用卡到期日期的应用程序但是andorid 5.0+日期时间选择器只有选择日期和日期的选项。 是否有任何图书馆,因为我找不到。 我也没有找到关于此的材料设计指南。

THX

2 个答案:

答案 0 :(得分:1)

提前做出一个名为Simple Date picker的修改日期选择器...使用类似于日期选择器的代码来显示月份和年份。您还可以为SimpleDatePickerDialog提供自己的主题。

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

答案 1 :(得分:0)

请试试这个

        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());
}

你可以试试这个

DatePicker dpDate = (DatePicker) findViewById(R.id.dpDate);

// Initialize Date Picker
int year    = dpDate.getYear();
int month   = dpDate.getMonth();
int day     = dpDate.getDayOfMonth();
dpDate.init(year, month, day, this);

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

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
        {
            int monthSpinnerId = Resources.getSystem().getIdentifier("month", "id", "android");
            if (monthSpinnerId != 0) 
            {
                View monthSpinner = dpDate.findViewById(monthSpinnerId);
                if (monthSpinner != null) 
                {
                    monthSpinner.setVisibility(View.GONE);
                }
            }
        }

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
        {
            int yearSpinnerId = Resources.getSystem().getIdentifier("year", "id", "android");
            if (yearSpinnerId != 0) 
            {
                View yearSpinner = dpDate.findViewById(yearSpinnerId);
                if (yearSpinner != null) 
                {
                    yearSpinner.setVisibility(View.GONE);
                }
            }
        }

了解更多信息

Hide Day, Month, or Year from DatePicker in Android 5.0+ Lollipop