2016-07-06 143 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

谢谢你的好解答! –