Android studio 最精簡(jiǎn)代碼的日期選擇器 只有 年 月
如果沒用到可以刪除對(duì)應(yīng)代碼
@RequiresApi(api = Build.VERSION_CODES.N)
? ? public static void openDateStartDialog(final TextView startdate, Context context) {
? ? ? ? final Calendar calendar = Calendar.getInstance();
? ? ? ? int yy = calendar.get(Calendar.YEAR);
? ? ? ? int mm = calendar.get(Calendar.MONTH);
? ? ? ? int dd = calendar.get(Calendar.DAY_OF_MONTH);
? ? ? ? new ContextThemeWrapper(context,android.R.style.Theme_Holo_Light_Dialog_NoActionBar);
? ? ? ? calendar.setTime(DateUtil.strToDate("yyyy-MM-dd", startdate.getText().toString().trim()));
? ? ? ? DatePickerDialog dlg = new DatePickerDialog(new ContextThemeWrapper(context,
? ? ? ? ? ? ? ? android.R.style.Theme_Holo_Light_Dialog_NoActionBar), null, yy, mm, dd) {
? ? ? ? ? ? @Override
? ? ? ? ? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? ? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? ? ? ? ? LinearLayout mSpinners = (LinearLayout) findViewById(getContext().getResources().getIdentifier("android:id/pickers", null, null));
? ? ? ? ? ? ? ? if (mSpinners != null) {
? ? ? ? ? ? ? ? ? ? NumberPicker mMonthSpinner = (NumberPicker) findViewById(getContext().getResources().getIdentifier("android:id/month", null, null));
? ? ? ? ? ? ? ? ? ? NumberPicker mYearSpinner = (NumberPicker) findViewById(getContext().getResources().getIdentifier("android:id/year", null, null));
? ? ? ? ? ? ? ? ? ? mSpinners.removeAllViews();
? ? ? ? ? ? ? ? ? ? if (mYearSpinner != null) {
? ? ? ? ? ? ? ? ? ? ? ? mSpinners.addView(mYearSpinner);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (mMonthSpinner != null) {
? ? ? ? ? ? ? ? ? ? ? ? mSpinners.addView(mMonthSpinner);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? View dayPickerView = findViewById(getContext().getResources().getIdentifier("android:id/day", null, null));
? ? ? ? ? ? ? ? if(dayPickerView != null){
? ? ? ? ? ? ? ? ? ? dayPickerView.setVisibility(View.GONE);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onDateChanged(DatePicker view, int year, int month, int day) {
? ? ? ? ? ? ? ? super.onDateChanged(view, year, month, day);
? ? ? ? ? ? ? ? setTitle("請(qǐng)選擇對(duì)應(yīng)的日期");
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? dlg.setTitle("請(qǐng)選擇對(duì)應(yīng)的日期");
? ? ? ? dlg.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
? ? ? ? ? ? ? ? startdate.setText(year + "-" + (month + 1 < 10 ? "0" + (month + 1) : (month + 1)) );
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? dlg.show();
? ? }
下面附上對(duì)應(yīng)的效果圖

常用的? 年月日 這里也貼出來了,當(dāng)然改一下上面的也行,但是這個(gè)感覺根據(jù)對(duì)應(yīng)版本號(hào)會(huì)好看一點(diǎn),也是塞入一個(gè)TextView和this就可以了,你可以放在點(diǎn)擊事件里面調(diào)用
使用方法:?
openDateStartDialog(tv_time, this);
? ? ?public static void openDateStartDialog(final TextView startdate,? Context context) {
? ? ? ? final Calendar localCalendar = Calendar.getInstance();
? ? ? ? localCalendar.setTime(DateUtil.strToDate("yyyy-MM-dd", startdate.getText().toString().trim()));
? ? ? ? new MonPickerDialog(context, new DatePickerDialog.OnDateSetListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
? ? ? ? ? ? ? ? localCalendar.set(Calendar.YEAR, year);
? ? ? ? ? ? ? ? localCalendar.set(Calendar.MONTH, month);
? ? ? ? ? ? ? ? localCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
? ? ? ? ? ? ? ? String day = dayOfMonth >= 10 ? dayOfMonth + "" : "0" + dayOfMonth;
? ? ? ? ? ? ? ? startdate.setText(year + "-" + (month + 1 < 10 ? "0" + (month + 1) : (month + 1)) + "-" + day);
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? ? ? ? ? localCalendar.get(Calendar.YEAR), localCalendar.get(Calendar.MONTH), localCalendar.get(Calendar.DAY_OF_MONTH)).show();
? ? }
? ? public static class MonPickerDialog extends DatePickerDialog {
? ? ? ? public MonPickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
? ? ? ? ? ? super(context, callBack, year, monthOfYear, dayOfMonth);
? ? ? ? }
? ? ? ? @Override
? ? ? ? public void onDateChanged(DatePicker view, int year, int month, int day) {
? ? ? ? ? ? super.onDateChanged(view, year, month, day);
? ? ? ? }
? ? }
