最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

Android studio 最精簡(jiǎn)代碼的日期選擇器 只有 年 月

2023-07-05 04:20 作者:翼618  | 我要投稿



如果沒用到可以刪除對(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);

? ? ? ? }

? ? }

效果圖2



Android studio 最精簡(jiǎn)代碼的日期選擇器 只有 年 月的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
吴川市| 张家港市| 丰镇市| 安仁县| 芦溪县| 施秉县| 隆德县| 伊通| 库伦旗| 寿宁县| 扶绥县| 泸西县| 灯塔市| 休宁县| 曲周县| 松江区| 绍兴县| 沭阳县| 固始县| 井冈山市| 云霄县| 乐平市| 伊春市| 保德县| 星座| 伊吾县| 金川县| 天门市| 简阳市| 项城市| 邹平县| 奉新县| 兴城市| 麦盖提县| 乐都县| 金阳县| 山东省| 磐安县| 金昌市| 福安市| 股票|