Android の DatePickerDialog で「年月」のみ選択させる方法
こんな Fragment を作れば良いと思う。
import android.app.DatePickerDialog; import android.app.Dialog; import android.content.Context; import android.content.res.Resources; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.View; import android.view.Window; import android.widget.DatePicker; import java.text.SimpleDateFormat; import java.util.Calendar; public class YearMonthPickerDialogFragment extends DialogFragment { private int year; private int month; private DatePickerDialog.OnDateSetListener onDateSetListener; public YearMonthPickerDialogFragment(int year, int month, DatePickerDialog.OnDateSetListener onDateSetListener) { this.year = year; this.month = month; this.onDateSetListener = onDateSetListener; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { YearMonthPickerDialog dialog = new YearMonthPickerDialog(getActivity(), onDateSetListener, year, month, 1); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); DatePicker datePicker = dialog.getDatePicker(); int dayId = Resources.getSystem().getIdentifier("day", "id", "android"); datePicker.findViewById(dayId).setVisibility(View.GONE); dialog.onDateChanged(datePicker, monthDescriptor.getYear(), monthDescriptor.getMonth(), 1); return dialog; } static class YearMonthPickerDialog extends DatePickerDialog { public YearMonthPickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { super(context, callBack, year, monthOfYear, dayOfMonth); } public YearMonthPickerDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { super(context, theme, callBack, year, monthOfYear, dayOfMonth); } @Override public void onDateChanged(DatePicker view, int year, int month, int day) { Calendar cal = Calendar.getInstance(); cal.set(year, month, day); String title = new SimpleDateFormat("yyyy-M").format(cal.getTime()); setTitle(title); } } }
参考
http://d.hatena.ne.jp/koko_u/20120225/1330187058
http://www.entrycoder.com/2013/05/how-to-show-only-month-and-year-fields.html