2

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

@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));
               */





    }
};

我只是想隐藏日期部分任何帮助都将非常感谢

4

6 回答 6

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();
于 2013-08-20T08:56:30.373 回答
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");
于 2013-08-21T08:00:53.857 回答
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);
于 2014-12-22T17:08:01.997 回答
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());
    }
}
于 2015-08-05T17:14:10.207 回答
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();
        }
    });
于 2017-04-18T17:21:37.773 回答
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();
于 2018-05-18T09:08:04.713 回答