如何在android中显示月份和年份的标题?

时间:2014-08-01 12:09:44

标签: android android-layout android-activity

我只需要在标题中显示月份和年份。怎么解决这个问题。我可以在日期选择器中显示,但标题显示为日。

    scanExpireDate = (CheckoutEditText) findViewById(R.id.scan_expire_date);
    scanExpireDate.setTextView();



public void onDatePickerClick(View view){
                Utils.customDatePicker(this, datePickerListener);
        }

        private final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
                String cardExpireDateFormt = Utils.getCardExpireDate(selectedYear,  selectedMonth);
                if(isCardScanned){
                    scanExpireDate.setText(cardExpireDateFormt);
                }else{
                    cardExpireDate.setText(cardExpireDateFormt);
                }
            }
        };

        public static DatePickerDialog customDatePicker(Activity activity, DatePickerDialog.OnDateSetListener datePickerListener) {

        Calendar c = Calendar.getInstance();
        int cardYear = c.get(Calendar.YEAR);
        int cardMonth = c.get(Calendar.MONTH);
        int cardDay = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(activity, datePickerListener, cardYear,cardMonth,cardDay);
        Date minDate = new Date();
        c.set(cardYear, cardMonth, cardDay - 1, 0, 0);
        minDate.setTime(c.getTimeInMillis());
        datePickerDialog.getDatePicker().setMinDate(minDate.getTime());

        try {
            Field[] datePickerDialogFields = datePickerDialog.getClass().getDeclaredFields();
            for (Field datePickerDialogField : datePickerDialogFields) {
                if (datePickerDialogField.getName().equals("mDatePicker")) {
                    datePickerDialogField.setAccessible(true);
                    DatePicker datePicker = (DatePicker) datePickerDialogField.get(datePickerDialog);
                    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);
                        }
                    }
                }
            }
            datePickerDialog.show();
        } catch (Exception ex) {
        }
        return datePickerDialog;
    }

如何删除标题中的日期?

它显示如下..    enter image description here

提前致谢

3 个答案:

答案 0 :(得分:1)

为实现这一目标,您需要自定义datepicker对话..

请参阅此链接

http://www.open-sourced.de/show_article.php?articleid=56

或者你可以简单地使用

datePickerDialog.setTitle()方法

希望它会帮助你

答案 1 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

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);
      setTitle(month+" "+year);
   }
 }

答案 2 :(得分:0)

我也有问题,但我找不到完美的解决方案。但我尝试使用

datePickerDialog.setCustomTitle(new TextView(context));

删除标题,但即使在顶部也有一行。

//更新 更完美的解决方案:

FrameLayout frameLayout = new FrameLayout(getContext());
frameLayout.setLayoutParams(new FrameLayout.LayoutParams(0, 0));
datePickerDialog.setCustomTitle(frameLayout);