2016-07-06 107 views
0

我是一名android開發人員。 我想使用DatePickerDialog。 但這個小部件顯示年 - 月。 我想顯示月 - 年。 如何?我想更改DatePickerDialog

+1

檢查http://stackoverflow.com/questions/14190968/android-format-date -ymm-dd-yyyy-date-datepicker – Evgeny

回答

0

試試這個 -

((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE); 

它適用於>= 11

如果您需要自定義選擇器按您的要求,然後更好地創建自己的使用NumberPickers,這裏是例子 -

public class MonthYearPickerDialog extends DialogFragment { 

    private static final int MAX_YEAR = 2099; 
    private DatePickerDialog.OnDateSetListener listener; 

    public void setListener(DatePickerDialog.OnDateSetListener listener) { 
    this.listener = listener; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    // Get the layout inflater 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    Calendar cal = Calendar.getInstance(); 

    View dialog = inflater.inflate(R.layout.date_picker_dialog, null); 
    final NumberPicker monthPicker = (NumberPicker) dialog.findViewById(R.id.picker_month); 
    final NumberPicker yearPicker = (NumberPicker) dialog.findViewById(R.id.picker_year); 

    monthPicker.setMinValue(1); 
    monthPicker.setMaxValue(12); 
    monthPicker.setValue(cal.get(Calendar.MONTH) + 1); 

    int year = cal.get(Calendar.YEAR); 
    yearPicker.setMinValue(year); 
    yearPicker.setMaxValue(MAX_YEAR); 
    yearPicker.setValue(year); 

    builder.setView(dialog) 
     // Add action buttons 
     .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int id) { 
      listener.onDateSet(null, yearPicker.getValue(), monthPicker.getValue(), 0); 
      } 
     }) 
     .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
      MonthYearPickerDialog.this.getDialog().cancel(); 
      } 
     }); 
    return builder.create(); 
    } 
} 

佈局 -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:orientation="horizontal"> 

     <NumberPicker 
      android:id="@+id/picker_year" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginEnd="20dp" 
      android:layout_marginRight="20dp"> 

     </NumberPicker> 

     <NumberPicker 
      android:id="@+id/picker_month" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

     </NumberPicker> 

    </LinearLayout> 
</LinearLayout> 

要顯示的對話框 -

MonthYearPickerDialog pd = new MonthYearPickerDialog(); 
pd.setListener(this); 
pd.show(getFragmentManager(), "MonthYearPickerDialog"); 

參考 - https://stackoverflow.com/a/31653048/1741586

希望它會工作:)

+0

謝謝你的回答。但我也想訂購 - >年 - 月(X)到月 - 年(O) –

+0

更新了我的答案,希望它對你有用:) – Neo

+0

謝謝你的好解答! –