在android中的DateTimePicker中显示Year

时间:2014-03-26 02:13:41

标签: android datetimepicker

我第一次发现代码在android中显示datetimepicker(仅显示年份和月份),但我真的很困惑如何只在我的日期时间选择器中显示年份,有人可以帮助我吗?因为我是编程的新手

package com.example.datepicker;
import java.lang.reflect.Field;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;

public class MainActivity extends Activity {
 static final int DATE_DIALOG_ID = 1;
 private int mYear;
 private int mMonth;
 private int mDay;
 private EditText etPickADate;

 @Override
 protected void onCreate(Bundle savedInstanceState) 
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      etPickADate = (EditText) findViewById(R.id.et_datePicker);
      etPickADate.setOnClickListener(new OnClickListener() 
      {

         @Override
         public void onClick(View arg0) 
         {
            showDialog(DATE_DIALOG_ID);
         }
       });    
  final Calendar c = Calendar.getInstance();
  mYear = c.get(Calendar.YEAR);
  mMonth = c.get(Calendar.MONTH);
 }

  DatePickerDialog.OnDateSetListener mDateSetListner = new OnDateSetListener() 
 {

      @Override
      public void onDateSet(DatePicker view, int year, int monthOfYear,
      int dayOfMonth) 
      {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDate();
      }
  };

 @Override
 protected Dialog onCreateDialog(int id) 
 {
  switch (id) 
  {
    case DATE_DIALOG_ID:
     /*
      * return new DatePickerDialog(this, mDateSetListner, mYear, mMonth,
      * mDay);
      */
   DatePickerDialog datePickerDialog = this.customDatePicker();
   return datePickerDialog;
   }
  return null;
 }

 protected void updateDate() {
  int localMonth = (mMonth + 1);
  String monthString = localMonth < 10 ? "0" + localMonth : Integer
    .toString(localMonth);
  String localYear = Integer.toString(mYear).substring(2);
  etPickADate.setText(new StringBuilder()
  // Month is 0 based so add 1
    .append(monthString).append("/").append(localYear).append(" "));
  showDialog(DATE_DIALOG_ID);
 }

 private DatePickerDialog customDatePicker() 
 {
    DatePickerDialog dpd = new DatePickerDialog(this, mDateSetListner, mYear, mMonth, mDay);
  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())||"mDaySpinner".equals(datePickerField
      .getName())) 
           {
               datePickerField.setAccessible(true);
               Object dayPicker = new Object();
               dayPicker = datePickerField.get(datePicker);
               ((View) dayPicker).setVisibility(View.GONE);
           }
        }
     }
   }
 } 
catch (Exception ex) 
{}
 return dpd;
}
}

感谢你能帮助我的人:D

1 个答案:

答案 0 :(得分:0)

要在DatePickerDialog中仅显示年份,您还需要使用反射为GONE NumberPicker设置可见性Month。这样做:

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);
   }else if("mMonthPicker".equals(datePickerField.getName())){
       // to hide month picker
       datePickerField.setAccessible(true);
       Object monthPicker = new Object();
       monthPicker = datePickerField.get(datePicker);
       ((View) monthPicker).setVisibility(View.GONE);
   }