我想在日期选择器对话框中禁用日期选择。我使用下面的行禁用但我得到崩溃(空指针异常)。请帮我解决这个问题
((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
完整代码:
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
tvDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
datePickerDialog.show();
答案 0 :(得分:0)
如果您使用的是DD / MM / YYYY
LinearLayout pickerParentLayout = (LinearLayout) datePicker.getChildAt(0);
LinearLayout pickerSpinnersHolder = (LinearLayout) pickerParentLayout.getChildAt(0);
NumberPicker picker = (NumberPicker) pickerSpinnersHolder.getChildAt(0);
picker.setVisibility(View.GONE);
NumberPicker选择器=(NumberPicker)pickerSpinnersHolder.getChildAt(0); 更改此行要隐藏的内容。