2012-09-17 183 views
0

我是Android新手,所以請原諒這樣一個簡單的問題。如何在Android日期選取器中僅設置月份和年份? 我不想在它的日期。請幫助。Android:僅有月份和年份的日期選擇器

+2

Android [文檔](http://developer.android.com/guide/topics/ui/controls/pickers.html)包含清晰的代碼和示例。你有沒有先訪問過這個鏈接? – AnujAroshA

+0

它沒有說隱藏日期。 – bhejaFry

+0

檢查這個答案: http://stackoverflow.com/questions/4122929/how-to-display-date-picker-for-android-with-only-month-and-year-fields/18167466#18167466 – mspapant

回答

1

據我所知,你不能用默認的日期選擇器

2

這裏做的是與月份和年份只有自定義日期選取器的代碼。在這裏,我點擊編輯文本框打開了日期選擇器。

public class MainActivity extends Activity { 
    static final int DATE_DIALOG_ID = 1; 
    private int mYear = 2013; 
    private int mMonth = 5; 
    private int mDay = 30; 
    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() { 

      @SuppressWarnings("deprecation") 
      @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; 
    } 

    @SuppressWarnings("deprecation") 
    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; 
    } 
} 
+0

如果有人想知道,這種方法使用反射來設置日期選擇器的可見性以完成。 –

+0

以上代碼不適用於android棒棒糖。 –

相關問題