Android DatePicker仅更改为月份和年份
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android DatePicker仅更改为月份和年份相关的知识,希望对你有一定的参考价值。
我有一个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();
;
谢谢
答案
尝试以下代码。它将显示仅包含年和月(无日)的DatePicker
private DatePickerDialog createDialogWithoutDateField()
DatePickerDialog dpd = new DatePickerDialog(this, null, 2014, 1, 24);
try
java.lang.reflect.Field[] datePickerDialogFields = dpd.getClass().getDeclaredFields();
for (java.lang.reflect.Field datePickerDialogField : datePickerDialogFields)
if (datePickerDialogField.getName().equals("mDatePicker"))
datePickerDialogField.setAccessible(true);
DatePicker datePicker = (DatePicker) datePickerDialogField.get(dpd);
java.lang.reflect.Field[] datePickerFields = datePickerDialogField.getType().getDeclaredFields();
for (java.lang.reflect.Field datePickerField : datePickerFields)
Log.i("test", datePickerField.getName());
if ("mDaySpinner".equals(datePickerField.getName()))
datePickerField.setAccessible(true);
Object dayPicker = datePickerField.get(datePicker);
((View) dayPicker).setVisibility(View.GONE);
catch (Exception ex)
return dpd;
此方法返回一个日期选择器对话框。因此,在按钮的onClick
方法中添加以下代码以显示对话框。
createDialogWithoutDateField().show();
另一答案
[我最近自己偶然发现了这个问题,因此我测试了本文的多个解决方案以及关于Stackoverflow的类似问题。
不幸的是,我没有找到适用于android 5+的解决方案
我最终实现了嵌入两个NumberPickers的自己的简单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");
另一答案
我不建议您使用反射来做这种事情。
有一种更简单,更漂亮的方法:
((ViewGroup) datePickerDialog.getDatePicker()).findViewById(Resources.getSystem().getIdentifier("day", "id", "android")).setVisibility(View.GONE);
请注意,.getDatePicker()
中的DatePickerDialog
方法适用于API LEVEL >= 11
。
此外,它不适用于API LEVEL> = 21。
另一答案
[[Stephan Klein答案的更多高级形式。
由于我需要设置。我还处理过[[february 28等日期,也处理过[[leap year。]]MonthYearPickerDialog年份可选
public class MonthYearPickerDialog extends DialogFragment
private DatePickerDialog.OnDateSetListener listener;
private int daysOfMonth = 31;
private NumberPicker monthPicker;
private NumberPicker yearPicker;
private NumberPicker dayPicker;
private Calendar cal = Calendar.getInstance();
public static final String MONTH_KEY = "monthValue";
public static final String DAY_KEY = "dayValue";
public static final String YEAR_KEY = "yearValue";
int monthVal = -1 , dayVal = -1 , yearVal =-1 ;
@Override
public void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
Bundle extras = getArguments();
if(extras != null)
monthVal = extras.getInt(MONTH_KEY , -1);
dayVal = extras.getInt(DAY_KEY , -1);
yearVal = extras.getInt(YEAR_KEY , -1);
public static MonthYearPickerDialog newInstance(int monthIndex , int daysIndex , int yearIndex)
MonthYearPickerDialog f = new MonthYearPickerDialog();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt(MONTH_KEY, monthIndex);
args.putInt(DAY_KEY, daysIndex);
args.putInt(YEAR_KEY, yearIndex);
f.setArguments(args);
return f;
public void setListener(DatePickerDialog.OnDateSetListener listener)
this.listener = listener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
//getDialog().setTitle("Add Birthday");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialog = inflater.inflate(R.layout.month_year_picker, null);
monthPicker = (NumberPicker) dialog.findViewById(R.id.picker_month);
yearPicker = (NumberPicker) dialog.findViewById(R.id.picker_year);
dayPicker = (NumberPicker) dialog.findViewById(R.id.picker_day);
monthPicker.setMinValue(1);
monthPicker.setMaxValue(12);
if(monthVal != -1)// && (monthVal > 0 && monthVal < 13))
monthPicker.setValue(monthVal);
else
monthPicker.setValue(cal.get(Calendar.MONTH) + 1);
monthPicker.setDisplayedValues(new String[]"Jan","Feb","Mar","Apr","May","June","July",
"Aug","Sep","Oct","Nov","Dec");
dayPicker.setMinValue(1);
dayPicker.setMaxValue(daysOfMonth);
if(dayVal != -1)
dayPicker.setValue(dayVal);
else
dayPicker.setValue(cal.get(Calendar.DAY_OF_MONTH));
monthPicker.setOnValueChangedListener(new NumberPick以上是关于Android DatePicker仅更改为月份和年份的主要内容,如果未能解决你的问题,请参考以下文章
jQuery UI datepicker:如何将下拉列表中的月份名称从短名称更改为长名称?
滚动视图中的 Android 5.0 Datepicker 不起作用(不滚动月份)