0

嗨,有什么正确的方法可以在 datepickerdialog 中隐藏日期选择器吗?我使用的是原生 android datepickerdialog,而不是自定义 datepickerdialog。希望有人可以提供帮助。

下面是我的 datepickerdialog 代码:

Button.OnClickListener monthOnClickListener
= new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
        DialogFragment newFragment = new SelectDateFragment();
        newFragment.show(getFragmentManager(), "DatePicker");
    }
};

public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        String year=yy+"", month, day;
        mm = mm+1;

        if (dd==1 || dd==2 || dd==3 || dd==4 || dd==5 || dd==6 || dd==7 || dd==8 || dd==9)
            day = "0"+ dd;
        else
            day = dd + "";

        if (mm==1 || mm==2 || mm==3 || mm==4 || mm==5 || mm==6 || mm==7 || mm==8 || mm==9)
            month = "0"+ mm;
        else
            month = mm  + "";
    }

}

我有谷歌隐藏日期选择器的方式,但它似乎没有任何效果。下面是我尝试过的代码。

public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);

         DatePickerDialog dpd = new DatePickerDialog(getActivity(), this,yy,mm, dd);
            try{
            java.lang.reflect.Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
            for (java.lang.reflect.Field datePickerDialogField : datePickerDialogFields) { 
                if (datePickerDialogField.getName().equals("mDatePicker")) {
                    datePickerDialogField.setAccessible(true);
                    DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
                    java.lang.reflect.Field[] datePickerFields = datePickerDialogField.getType().getDeclaredFields();
                    for (java.lang.reflect.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;
        //return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }
4

1 回答 1

0

带有“mDatePicker”的alnog也使用“mDaySpinner”这个

于 2013-03-23T17:02:45.580 回答