- /**
- * 从当前Dialog中查找DatePicker子控件
- *
- * @param group
- * @return
- */
- private DatePicker findDatePicker(ViewGroup group) {
- if (group != null) {
- for (int i = 0, j = group.getChildCount(); i < j; i++) {
- View child = group.getChildAt(i);
- if (child instanceof DatePicker) {
- return (DatePicker) child;
- } else if (child instanceof ViewGroup) {
- DatePicker result = findDatePicker((ViewGroup) child);
- if (result != null)
- return result;
- }
- }
- }
- return null;
- }
代码说明:
通过断点也看到Dialog的ContentView里有DatePicker子控件,这里通过遍历的办法来查找这个控件。
通过断点也看到Dialog的ContentView里有DatePicker子控件,这里通过遍历的办法来查找这个控件。
- final Calendar cal = Calendar.getInstance();
- mDialog = new CustomerDatePickerDialog(getContext(), this,
- cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
- cal.get(Calendar.DAY_OF_MONTH));
- mDialog.show();
- DatePicker dp = findDatePicker((ViewGroup) mDialog.getWindow().getDecorView());
- if (dp != null) {
- ((ViewGroup) dp.getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
- }
- class CustomerDatePickerDialog extends DatePickerDialog {
- public CustomerDatePickerDialog(Context context,
- OnDateSetListener callBack, int year, int monthOfYear,
- int dayOfMonth) {
- super(context, callBack, year, monthOfYear, dayOfMonth);
- }
- @Override
- public void onDateChanged(DatePicker view, int year, int month, int day) {
- super.onDateChanged(view, year, month, day);
- mDialog.setTitle((month + 1) + "月" + day + "日");
- }
- }