Android studio 最精简代码的日期选择器 只有 年 月 全球观焦点
哔哩哔哩 2023-07-05 04:26:51

如果没用到可以删除对应代码


(资料图片仅供参考)

@RequiresApi(api = _)

public static void openDateStartDialog(final TextView startdate, Context context) {

final Calendar calendar = ();

int yy = ();

int mm = ();

int dd = (_OF_MONTH);

new ContextThemeWrapper(context,_Holo_Light_Dialog_NoActionBar);

(("yyyy-MM-dd", ().toString().trim()));

DatePickerDialog dlg = new DatePickerDialog(new ContextThemeWrapper(context,

_Holo_Light_Dialog_NoActionBar), null, yy, mm, dd) {

@Override

protected void onCreate(Bundle savedInstanceState) {

(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));

();

if (mYearSpinner != null) {

(mYearSpinner);

}

if (mMonthSpinner != null) {

(mMonthSpinner);

}

}

View dayPickerView = findViewById(getContext().getResources().getIdentifier("android:id/day", null, null));

if(dayPickerView != null){

();

}

}

@Override

public void onDateChanged(DatePicker view, int year, int month, int day) {

(view, year, month, day);

setTitle("请选择对应的日期");

}

};

("请选择对应的日期");

(new () {

@Override

public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

(year + "-" + (month + 1 < 10 ? "0" + (month + 1) : (month + 1)) );

}

});

();

}

下面附上对应的效果图

常用的  年月日 这里也贴出来了,当然改一下上面的也行,但是这个感觉根据对应版本号会好看一点,也是塞入一个TextView和this就可以了,你可以放在点击事件里面调用

使用方法: 

openDateStartDialog(tv_time, this);

public static void openDateStartDialog(final TextView startdate,  Context context) {

final Calendar localCalendar = ();

(("yyyy-MM-dd", ().toString().trim()));

new MonPickerDialog(context, new () {

@Override

public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

(, year);

(, month);

(_OF_MONTH, dayOfMonth);

String day = dayOfMonth >= 10 ? dayOfMonth + "" : "0" + dayOfMonth;

(year + "-" + (month + 1 < 10 ? "0" + (month + 1) : (month + 1)) + "-" + 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) {

(view, year, month, day);

}

}

Android studio 最精简代码的日期选择器 只有 年 月 全球观焦点

2023-07-05

刘诗雯能否完成大满贯?关键在与陈梦和伊藤美诚! 环球快报

2023-07-05

为啥现在网上都在劝退设计院

2023-07-04

报道:为何机床会越做越烂?原因在这里!!!

2023-07-04

蓝戟英特尔锐炫 A770 显卡降至 2299 元,16GB 256bit 大显存_环球新动态

2023-07-04

让“养老”变“享老” 我国多地积极创新养老新模式_全球微头条

2023-07-04

沪宁沿江高铁联调联试试验速度达385公里每小时 全球快报

2023-07-04

天天播报:借给的反义词_给的反义词是什么标准答案

2023-07-04

猫忍之心将登陆任天堂Switch平台

2023-07-04

莫锦洪(关于莫锦洪的简介) 讯息

2023-07-04

华夏红利混合月度点评

2023-07-04

烟台大学附属中学石明校区开展反诈防骗安全教育宣讲活动

2023-07-04

“迎杭州亚运会趣味跑”东帝汶站热烈开跑

2023-07-04

避暑度假到贵州丨色彩斑斓!夏日来感受贵州的“多巴胺美景”吧! 环球快讯

2023-07-04

苹果天气显示桌面 苹果天气 基本情况讲解 全球快播报

2023-07-04

中国卫星7月4日快速上涨

2023-07-04

债券通张漪: 债券通成为中国金融市场对外开放的标杆性业务

2023-07-04

解读2023MWC上海大会:5G、元宇宙、大模型领域有新变化 环球精选

2023-07-04

这个货物是到付 到付的尾款是3500 但是之前已经付过400

2023-07-04

13pm电池多少(苹果官网13pm啥时候下架的)_天天百事通

2023-07-04