从日期选择器隐藏日期

时间:2013-08-05 11:04:39

标签: android datepicker

目前我正在使用日期选择器本机,但我想以这样的方式更改它,我只需要一个月和一年 如何修改我的代码是

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
       // set date picker as current date
       return new DatePickerDialog(this, datePickerListener, 
                     year, month,day);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener datePickerListener
            = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {

    /*  year = selectedYear;
        month = selectedMonth;
        day = selectedDay;*/
        mynewYear=selectedYear;
        myNewMonth=selectedMonth;
        myNewDay=selectedDay;
        try {
            checkDate();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        /*time_date_TV.setText(new StringBuilder()
          .append(myNewMonth + 1).append("/")
          .append(myNewDay ).append("/").append(mynewYear));
               */





    }
};

我只想隐藏日期部分任何帮助都将被感谢完整

6 个答案:

答案 0 :(得分:2)

脑力激荡半天后,我找到了这个解决方案,这可能有助于其他有这种情况

我的日期选择器的自定义类是以下每次用户更改月/年时将更新标题中的日期

class CustomDatePickerDialog extends DatePickerDialog implements OnDateChangedListener {

private DatePickerDialog mDatePicker;

@SuppressLint("NewApi")
public CustomDatePickerDialog(Context context,int theme, OnDateSetListener callBack,
        int year, int monthOfYear, int dayOfMonth) {
    super(context, theme,callBack, year, monthOfYear, dayOfMonth);
    mDatePicker = new DatePickerDialog(context,theme,callBack, year, monthOfYear, dayOfMonth);

    mDatePicker.getDatePicker().init(2013, 7, 16, this);

    updateTitle(year, monthOfYear);

}
public void onDateChanged(DatePicker view, int year,
        int month, int day) {
    updateTitle(year, month);
}
private void updateTitle(int year, int month) {
    Calendar mCalendar = Calendar.getInstance();
    mCalendar.set(Calendar.YEAR, year);
    mCalendar.set(Calendar.MONTH, month);
//       mCalendar.set(Calendar.DAY_OF_MONTH, day);
        mDatePicker.setTitle(getFormat().format(mCalendar.getTime()));

}   

public DatePickerDialog getPicker(){

    return this.mDatePicker;
}
    /*
     * the format for dialog tile,and you can override this method
     */
public SimpleDateFormat getFormat(){
    return new SimpleDateFormat("MMM, yyyy");
};   
}

以下是将日期选择器对话框

隐藏日期及其微调器的代码
CustomDatePickerDialog dp = new CustomDatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog,  datePickerListener, year, month, day);

               DatePickerDialog obj = dp.getPicker();
              try{
                         Field[] datePickerDialogFields = obj.getClass().getDeclaredFields();
                         for (Field datePickerDialogField : datePickerDialogFields) { 
                             if (datePickerDialogField.getName().equals("mDatePicker")) {
                                 datePickerDialogField.setAccessible(true);
                                 DatePicker datePicker = (DatePicker) datePickerDialogField.get(obj);
                                 Field datePickerFields[] = datePickerDialogField.getType().getDeclaredFields();
                                 for (Field datePickerField : datePickerFields) {
                                    if ("mDayPicker".equals(datePickerField.getName()) || "mDaySpinner".equals(datePickerField
                                      .getName())) {
                                       datePickerField.setAccessible(true);
                                       Object dayPicker = new Object();
                                       dayPicker = datePickerField.get(datePicker);
                                       ((View) dayPicker).setVisibility(View.GONE);
                                    }
                                 }
                              }

                           }
                         }catch(Exception ex){
                         }
              obj.show();

答案 1 :(得分:2)

简单地说 - 只是在datePickerDialog中隐藏NumberPicker 这是我项目的代码

public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final Calendar c = Calendar.getInstance();

        DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, c.get(Calendar.YEAR),
                c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));

        dialog.getDatePicker().setSpinnersShown(true);

        // hiding calendarview and daySpinner in datePicker
            dialog.getDatePicker().setCalendarViewShown(false);

            LinearLayout pickerParentLayout = (LinearLayout) dialog.getDatePicker().getChildAt(0);

            LinearLayout pickerSpinnersHolder = (LinearLayout) pickerParentLayout.getChildAt(0);

            pickerSpinnersHolder.getChildAt(0).setVisibility(View.GONE);


        dialog.setTitle("Pick a date");
        return dialog;
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {

        Calendar result = Calendar.getInstance();

        result.set(Calendar.YEAR, yy);
        result.set(Calendar.MONDAY, mm);
        result.set(Calendar.DAY_OF_MONTH, dd);

        if (currentWeek != -1) {
            updateDateWeek(result.getTimeInMillis());
        } else {
            updateDate(result.getTimeInMillis());
        }
    }
}

显示选择器只需致电

DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getActivity().getSupportFragmentManager(), "DatePicker");

答案 2 :(得分:2)

这可能对你有用..如果你只做英文..也可以根据要求进行修改。

Locale.setDefault(Locale.UK);

这将使日期选择器以DD / MM / YYYY

的形式出现

以下代码会删除日期分页器

LinearLayout pickerParentLayout =
(LinearLayout) mDatePicker.getChildAt(0);
LinearLayout pickerSpinnersHolder = (LinearLayout) pickerParentLayout.getChildAt(0);
NumberPicker picker = (NumberPicker) pickerSpinnersHolder.getChildAt(0);
picker.setVisibility(View.GONE);

答案 3 :(得分:1)

在尝试了几种不同的方法后,这对我有用。

 datePicker = (DatePicker) findViewById(R.id.date_picker);

 public void hideDay() {
    try {
        java.lang.reflect.Field[] f = datePicker.getClass().getDeclaredFields();
        for (java.lang.reflect.Field field : f) {
            if (field.getName().equals("mDayPicker") || field.getName().equals("mDaySpinner")) {
                field.setAccessible(true);
                Object dmPicker = new Object();
                dmPicker = field.get(datePicker);
                ((View) dmPicker).setVisibility(View.GONE);
            }
        }
    }
    catch (SecurityException e) {
        TLog.d("ERROR", e.getMessage());
    }
    catch (IllegalArgumentException e) {
        TLog.d("ERROR", e.getMessage());
    }
    catch (IllegalAccessException e) {
        TLog.d("ERROR", e.getMessage());
    }
}

答案 4 :(得分:0)

我修改了一些东西,比如: 制作风格的android默认Theme_Holo_Dialog并从可见性中删除当天。

我的代码经过少量更改后:

pick_expire_date.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Get Current Date
            final Calendar c = Calendar.getInstance();
            cyear = c.get(Calendar.YEAR);
            cmonth = c.get(Calendar.MONTH);
            cday = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog datePickerDialog = new DatePickerDialog(
                    CheckoutPaymentActivity.this,
                    android.R.style.Theme_Holo_Dialog,
                    new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {
                            pick_expire_date.setText((monthOfYear + 1)+"/"+year);
                            mMonth = monthOfYear + 1;
                            mYear = year;
                        }
                    },
                    cyear, cmonth, cday);
            datePickerDialog.getDatePicker().findViewById(getResources().getIdentifier("day","id","android")).setVisibility(View.GONE);
            datePickerDialog.show();
        }
    });

答案 5 :(得分:0)

http://learnlinky.com/2016/12/disable-day-selection-android-datepicker/

这对我有用:)

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();