在 Android DatePicker 中隐藏年份字段? [英] Hide Year field in Android DatePicker?

查看:19
本文介绍了在 Android DatePicker 中隐藏年份字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个基本的 DatePicker,我想弄清楚如何隐藏 DatePickerDialog 上的 Year 字段,以便只有 Month 和 Day 可见.我不介意年份的底层代码仍然存在,我只想隐藏对话框中的年份字段.你知道:

I'm using a basic DatePicker and I'm trying to figure out how to hide the Year field on the DatePickerDialog so that only the Month and Day are visible. I don't mind that the underlying code for the year would still be there, I'd just like to hide the Year Field in the Dialog. You know with something like:

((View) myYear).setVisibility(View.GONE);

我知道这是行不通的,因为 myYear 是一个 int 而不是 View,而是类似的东西.可能吗?

which I know doesn't work because myYear is a int not a View, but something along those lines. Is it possible?

推荐答案

我发现实现 DatePicker 的一个超级简单的方法是在 xml 中调用它:

A super easy way that I found to implement a DatePicker is to call it in xml:

    <DatePicker
    android:id="@+id/thePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

然后在java中隐藏您想要的任何字段(在本例中为年份):

and then hide whichever field you want (in this case the year) in java:

    picker = (DatePicker) findViewById(R.id.thePicker);
    try {
        Field f[] = picker.getClass().getDeclaredFields();
        for (Field field : f) {
            if (field.getName().equals("mYearPicker")) {
                field.setAccessible(true);
                Object yearPicker = new Object();
                yearPicker = field.get(picker);
                ((View) yearPicker).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());
    }

这篇关于在 Android DatePicker 中隐藏年份字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆