我有一个 DatePickerDialog,我只想查看月份和年份。如何更改此代码?
public void chooseDate2(View v) {
new DatePickerDialog(
act.this,
d1,
dateAndTime1.get(Calendar.YEAR) + 2,
dateAndTime1.get(Calendar.MONTH),
dateAndTime1.get(Calendar.DAY_OF_MONTH)
).show();
}
private void updateLabel2() {
scadenza.setText(fmtDateAndTime.format(dateAndTime1.getTime()));
}
DatePickerDialog.OnDateSetListener d1=new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
dateAndTime1.set(Calendar.YEAR, year);
dateAndTime1.set(Calendar.MONTH, monthOfYear);
dateAndTime1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel2();
}
};
谢谢
最佳答案
由于我最近自己偶然发现了这个问题,因此我测试了这篇文章中的多个解决方案以及 Stackoverflow 上的类似问题。
不幸的是,我没有找到特别适用于 Android 5+ 的有效解决方案
我最终实现了我自己的嵌入两个 NumberPicker 的简单 DialogFragment。这应该与 3.0 及更高版本的所有版本兼容。
代码如下:
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(0);
monthPicker.setMaxValue(11);
monthPicker.setValue(cal.get(Calendar.MONTH));
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_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp">
</NumberPicker>
<NumberPicker
android:id="@+id/picker_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</NumberPicker>
</LinearLayout>
</LinearLayout>
要显示布局,请使用:
MonthYearPickerDialog pd = new MonthYearPickerDialog();
pd.setListener(this);
pd.show(getFragmentManager(), "MonthYearPickerDialog");
关于Android DatePicker 更改为仅月和年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21321789/