Android的 - 在datepickerdialog隐藏日期字段(Android - Hide

2019-06-24 14:40发布

如何隐藏/在datepickerdialog删除日期字段。 其实我的要求是要使用本机日期选取器对话框,但它显示了日,月,年。 但按我的要求,我只需要月份和年份。 所以我想隐藏或从视图中删除当天现场。

任何帮助将非常有用。

Answer 1:

Private DatePickerDialog createDialogWithoutDateField(){

    DatePickerDialog dpd = new DatePickerDialog(_activity, ExpiryDateSetListener,cyear,cmonth, cday);
    try{
    Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
    for (Field datePickerDialogField : datePickerDialogFields) { 
        if (datePickerDialogField.getName().equals("mDatePicker")) {
            datePickerDialogField.setAccessible(true);
            DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
            Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
            for (Field datePickerField : datePickerFields) {
               if ("mDayPicker".equals(datePickerField.getName())) {
                  datePickerField.setAccessible(true);
                  Object dayPicker = new Object();
                  dayPicker = datePickerField.get(datePicker);
                  ((View) dayPicker).setVisibility(View.GONE);
               }
            }
         }

      }
    }catch(Exception ex){
    }
  return dpd;

}

使用上面的代码隐藏在DatePickerDialog日字段。

请参阅本LINK



Answer 2:

我不推荐使用反射做这种事情。

还有一个更简单,更漂亮的方式这样做:

((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);

要知道, .getDatePicker()从法DatePickerDialog工作在API LEVEL >= 11



Answer 3:

源代码DatePickerDatePickerDialog供您克隆,重构到自己的包,并修改,以适应。



Answer 4:

((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE); 

这工作正常



Answer 5:

 Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_DARK,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {

                        txtMonth.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

                    }
                }, mYear, mMonth, mDay);
        ((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
        datePickerDialog.show();

请参阅此链接上日期选择器例如禁用择日



Answer 6:

    private Calendar mCalendar;
    mCalendar = Calendar.getInstance();// Initialize Calendar

dialog_date_picker.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corners"
android:orientation="vertical"
android:padding="8dp">
<DatePicker
    android:id="@+id/date_picker"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="4"
    android:calendarViewShown="false"
    android:datePickerMode="spinner" />
<Button
    android:id="@+id/date_time_set"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@color/colorPrimary"
    android:text="Set"
    android:textColor="@color/colorWhite" /></LinearLayout>

DialogMethod呼吁点击:

private void ExpiryDialog() {
    System.out.println("Inside Dialog Box");
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_date_picker);
    dialog.show();
    final DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.date_picker);
    date_time_set = (Button) dialog.findViewById(R.id.date_time_set);
    datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), null);

    LinearLayout ll = (LinearLayout) datePicker.getChildAt(0);
    LinearLayout ll2 = (LinearLayout) ll.getChildAt(0);
    ll2.getChildAt(1).setVisibility(View.INVISIBLE);

    date_time_set.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view){
            dialog.dismiss();
            Integer month = datePicker.getMonth()+1;
            Integer year = datePicker.getYear();
            expMonth = (month.toString().length()   == 1 ? "0"+month.toString():month.toString());
            expYear = year.toString();
            etExpiryDate.setText(expMonth + "/" + expYear);
        }
    });
}

此代码和隐藏一天一个datepicker,这样我就可以显示卡到期日。 请享用!!



Answer 7:

我不认为你需要去深,除非你想定制..还有就是:

mDatePicker.getDatePicker().setCalendarViewShown(false);

这是一个非常合理的调用删除日历段。



文章来源: Android - Hide date field in datepickerdialog