效果图:
1.主要引用 checkdate
//添加自定义日期选择 implementation project(path: ':checkdate')
2.activity调用
package com.ruidde.csdntestdemo; import android.annotation.SuppressLint; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.zdmtech.checkdate.builder.TimePickerBuilder; import com.zdmtech.checkdate.listener.OnTimeSelectListener; import com.zdmtech.checkdate.utils.DateUtil; import com.zdmtech.checkdate.view.TimePickerView; import com.zdmtech.checkdate.view.WheelTime; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class MainActivity extends AppCompatActivity { private Context mContext; private TextView tv_content; private Button btn_picker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; initView(); initDate(); } public void initView() { tv_content = findViewById(R.id.tv_content); btn_picker = findViewById(R.id.btn_picker); } @SuppressLint("SimpleDateFormat") Calendar calendar ; public void initDate(){ /** * 自定义结束时间 * */ // String str="2022-06-24"; // SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd"); // try { // Date date = sdf.parse(str); // calendar = Calendar.getInstance(); // calendar.setTime(date); // } catch (ParseException e) { // e.printStackTrace(); // } /** * 自定义开始结束时间 * */ // WheelTime.DEFAULT_START_YEAR = 2000; // WheelTime.DEFAULT_END_YEAR = 2050; btn_picker.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean[] arr = new boolean[]{true, true, true, true, true, false}; Calendar startDate = Calendar.getInstance(); startDate.add(Calendar.YEAR, -5); //当前年数减去的值后作为开始年数 Calendar endDate = Calendar.getInstance(); TimePickerView pickerView = new TimePickerBuilder(mContext, new OnTimeSelectListener() { @Override public void onTimeSelect(Date date, View v) { String eventTime = DateUtil.dateToString(date, DateUtil.YMDHM); tv_content.setText(eventTime); } }).setType(arr) .isCyclic(false) //是否循环 .setDate(endDate) .setRangDate(startDate, endDate) //不设置隐藏该项显示数据是自定义的开始与结束时间(1900-2100) // .setRangDate(startDate, calendar) //calendar自定义结束日期 .build(); pickerView.show(); } }); } }
3.如果设置当日时间为全天而不是到当前时间可以在
中做修改
} else { //年份发生变化 int dateMonth = (Calendar.getInstance().get(Calendar.MONTH)+1); int dateDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); int dateHourse = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); int dateMinute = Calendar.getInstance().get(Calendar.MINUTE); int currentMonth = (wv_month.getCurrentItem() + 1); int currentDay = (wv_day.getCurrentItem() + 1); if (currentMonth == dateMonth){ //当月 if (currentDay == dateDay){ //当日 wv_hours.setAdapter(new NumericWheelAdapter(0, dateHourse)); //设置的结束小时 wv_minutes.setAdapter(new NumericWheelAdapter(0, dateMinute)); //设置的结束分钟 }else { //不同日 wv_hours.setAdapter(new NumericWheelAdapter(0, 23)); wv_minutes.setAdapter(new NumericWheelAdapter(0, 59)); } }else { //不同月 wv_hours.setAdapter(new NumericWheelAdapter(0, 23)); wv_minutes.setAdapter(new NumericWheelAdapter(0, 59)); } }
4.源码请点击链接下载:https://download.csdn.net/download/yyxhzdm/12888757
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119203.html