【发布时间】:2015-08-27 16:28:54
【问题描述】:
我一直在寻找一种解决方案来从 DatePicker 中隐藏以下任何一个微调器。对于Android 5.0,内部变量已更改,在我的情况下,更新到我的设备后再次可见日和月旋转器。
Android 4.4 和 Kitkat 解决方案
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);
Field f[] = dpDate.getClass().getDeclaredFields();
for (Field field : f)
{
// Hides the DAY spinner
if(field.getName().equals("mDayPicker") || field.getName().equals("mDaySpinner"))
{
field.setAccessible(true);
Object dayPicker = new Object();
dayPicker = field.get(dpDate);
((View) dayPicker).setVisibility(View.GONE);
}
// Hides the MONTH spinner
if(field.getName().equals("mMonthPicker") || field.getName().equals("mMonthSpinner"))
{
field.setAccessible(true);
Object monthPicker = new Object();
monthPicker = field.get(dpDate);
((View) monthPicker).setVisibility(View.GONE);
}
// Hides the YEAR spinner
if(field.getName().equals("mYearPicker") || field.getName().equals("mYearSpinner"))
{
field.setAccessible(true);
Object yearPicker = new Object();
yearPicker = field.get(dpDate);
((View) myearPicker).setVisibility(View.GONE);
}
}
Android 5.0+ Lollipop 解决方案
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);
}
}
}
我在这里找到了解决此问题的上述方法: Custom Date Picker Dialog in Android Lollipop
我想确保它易于搜索,以便其他人可以从中受益,所以我创建了这个新帖子。 (解决方案花了我几个小时才找到并理解,所以我希望能帮助别人)。
我已经对此进行了测试,并且效果很好。事实上,如果你把 Lollipop 的解决方案放在前面,然后在代码中,把 Kitkat 的解决方案放在下面,那么它对两个版本都兼容而不会干扰。 (确保使用 try/catch ;)
编辑 7.22.2015: 我认为如果使用 DatePicker 很明显需要对其进行初始化。我包含的代码表明您需要在运行其余代码(在两种情况下)之前初始化 DatePicker,否则您的视图将抛出 NullPointerException。这包括 yearSpinner 抛出一个空值。此外,您应该至少有一个视图显示日期,不要隐藏所有3,即,不要在同一时间隐藏一天,月份和年度。
【问题讨论】:
-
在此代码中“查看”始终为空..
-
你必须更具体地说明你在说什么......
-
我已经使用了上面的代码,我得到“yearSpinner”为空。应该是什么问题?
-
我已经对上面的代码进行了编辑,以解释为什么你们都得到了 NullPointerException。确保在尝试隐藏视图之前初始化 DatePicker,否则它将不知道该怎么做。看看上面的编辑。我还要说,您需要在 onCreate(对于 Activity)或 onCreateView(对于 Fragment)或 onCreateDialog(对于 DialogFragment)中声明 DatePicker。在声明之后在单独的方法中将其初始化。
标签: android datepicker