我是一名android开发人员。 我想使用DatePickerDialog。 但这个小部件显示年 - 月。 我想显示月 - 年。 如何?我想更改DatePickerDialog
0
A
回答
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
希望它会工作:)
相关问题
- 1. DialogFragment DatePickerDialog不返回更改日期
- 2. 更改DatePickerDialog的按钮文本
- 3. 如何更改DatePickerDialog的区域设置?
- 4. 在DatePickerDialog中更改日期格式?
- 5. DatePickerDialog
- 6. 我想改变
- 7. 我想将按钮更改为禁用
- 8. 我想点击更改课程“活动”,
- 9. 我想要动态更改UIDatePicker的datePickerMode
- 10. 我想在Facebook中更改网址。
- 11. 我想更改索引中的网址
- 12. 我想更改UITextView的字体大小
- 13. iphone uibutton:我想更改UIControlStateNormal图片
- 14. 我想更改页面标题位置
- 15. Android:如何更改Theme.Holo.Light.Dialog.MinWidth中的DatePickerDialog颜色?
- 16. Android:在DatePickerDialog/TimePickerDialog中更改按钮文本
- 17. 更改Android日期,日期和月份的显示格式DatePickerDialog
- 18. 我想改变UWP
- 19. 我想改变ringertonevolume
- 20. 我想改进我的SQL
- 21. android DatePickerDialog
- 22. 如何改变字体在Android的DatePickerDialog
- 23. 我想在同一窗口中更改URL或修改网址
- 24. 如何更新DatePickerDialog中的EditText值
- 25. 我想在joomla中更改我的字段名称
- 26. 我想看看旧的提交,而不是更改我的头
- 27. 更改accessoy图像的作品,但我想要更改plist BOOL值
- 28. 想改善我的观点
- 29. 改造,我想要求@GET
- 30. setMaxDate到DatePickerDialog
检查http://stackoverflow.com/questions/14190968/android-format-date -ymm-dd-yyyy-date-datepicker – Evgeny