低版本实现DatepickerDialog效果

低版本无法使用DatepickerDialog

可以将Datepicker嵌入到对话框实现

AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = View.inflate(this, R.layout.setmessage_aty_datedialog, null);
final DatePicker datePicker = (DatePicker) view.findViewById(R.id.SetMessage_aty_datedialog_dp);
builder.setView(view);

builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        int year = datePicker.getYear();
        int monthTemp = datePicker.getMonth() + 1;
        int day = datePicker.getDayOfMonth();

//转化为日期格式
        cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, monthTemp);
        cal.set(Calendar.DAY_OF_MONTH, day);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        tv_bir.setText(simpleDateFormat.format(cal.getTime()));
        dialog.cancel();
    }
});
Dialog dialog = builder.create();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.alpha = 1f;
dialog.getWindow().setAttributes(lp);
dialog.show();

 

 

 

扫描二维码关注公众号,回复: 1868752 查看本文章

android:calendarViewShown="false"
android:datePickerMode="spinner"

//可以只显示日期选择

猜你喜欢

转载自blog.csdn.net/qq_39650441/article/details/80682772
今日推荐