隐藏在Android中的DatePicker年场? [英] Hide Year field in Android DatePicker?
问题描述
我使用的是基本的DatePicker,我试图找出如何隐藏年字段的DatePickerDialog这样只有月和日是可见的。我不介意底层$ C $下年将依然存在,我只是想隐藏年字段的对话框。你知道的东西,如:
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不是一个视图,但类似的规定。这可能吗?
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屋!