仿ios滚轮日期筛选

仿ios滚轮日期筛选_第1张图片
效果图

仿ios滚轮,根据 type 可自主选择时间显示的类型,具体见 MainActivity 代码注释

依赖两个包,可以 点我下载,需要解压

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private TimePickerView pvTime;
    /**
     * 可自主选择时间显示的类型
     * 只显示年则设置成 true, false, false, false, false, false 即可
     * 设置成 true, true, true, true, true, true 则年月日时分秒都显示
     */
    boolean[] type = {true, true, true, true, true, true};
    private TextView tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initTimePicker();
        Button btn_Time = (Button) findViewById(R.id.btn_Time);
        tv = findViewById(R.id.tv);
        btn_Time.setOnClickListener(this);
    }

    private void initTimePicker() {//Dialog 模式下,在底部弹出

        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                tv.setText(getTime(date));
            }
        })
                .setTimeSelectChangeListener(  new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                    }
                })
                .setType(type)
                .build();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_Time && pvTime != null) {
            pvTime.show(v);//弹出时间选择器
        }
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        return DateUtil.getPickedYear(date)+"年"+DateUtil.getPickedMonth(date)+"月"+
                DateUtil.getPickedDay(date)+"日"+DateUtil.getPickedHour(date)+":"
                +DateUtil.getPickedMinute(date)+":"+DateUtil.getPickedSecond(date);
    }
}
/**
 * 时间处理
 */
public class DateUtil {

    /**
     * 格式化日期:比如:2018-08-07 13:50:10
     *
     * @param time
     * @return
     */
    public static String formatTime(String time) {
        if (!TextUtils.isEmpty(time)) {
            time = time.trim();
            if (time.contains(" ")) {
                String[] timeArray = time.split(" ");
                return timeArray[0];
            }else {
                return time;
            }
        }
        return "";
    }

    /**
     * 保留两位小数
     *
     * @return
     */
    public static String formatFloat(String numStr) {
        if (!TextUtils.isEmpty(numStr)) {
            DecimalFormat df = new DecimalFormat("0.00");
            float f = Float.parseFloat(numStr);
            return df.format(f);
        }
        //numStr不合法则返回默认值
        return "0.00";
    }

    /**
     * 获取现在时间
     *
     * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
     */
  public static String formatDate(Date date) {
      if (date == null){
          return "";
      }
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return sdf.format(date);
  }

    /**
     * 获取选择的年:日期格式是固定的
     * @param date
     * @return
     */
    public static String getPickedYear(Date date){
        String time = formatDate(date);
        String splitDate = time.split(" ")[0];
        return splitDate.substring(0,splitDate.indexOf("-"));
    }

    /**
     * 获取选择的月:日期格式是固定的
     * @param date
     * @return
     */
    public static String getPickedMonth(Date date){
        String time = formatDate(date);
        String splitDate = time.split(" ")[0];
        return splitDate.substring(splitDate.indexOf("-") + 1,splitDate.lastIndexOf("-"));
    }

    /**
     * 获取选择的日:日期格式是固定的
     * @param date
     * @return
     */
    public static String getPickedDay(Date date){
        String time = formatDate(date);
        String splitDate = time.split(" ")[0];
        return splitDate.substring(splitDate.lastIndexOf("-")+1);
    }

    /**
     * 获取选择的时:日期格式是固定的
     * @param date
     * @return
     */
    public static String getPickedHour(Date date){
        String time = formatDate(date);
        String splitDate = time.split(" ")[1];
        return splitDate.substring(0,splitDate.indexOf(":"));
    }

    /**
     * 获取选择的分:日期格式是固定的
     * @param date
     * @return
     */
    public static String getPickedMinute(Date date){
        String time = formatDate(date);
        String splitDate = time.split(" ")[1];
        return splitDate.substring(splitDate.indexOf(":") + 1,splitDate.lastIndexOf(":"));
    }

    /**
     * 获取选择的秒:日期格式是固定的
     * @param date
     * @return
     */
    public static String getPickedSecond(Date date){
        String time = formatDate(date);
        String splitDate = time.split(" ")[1];
        return splitDate.substring(splitDate.lastIndexOf(":")+1);
    }
}

activity_main.xml



    

    

记得依赖包 implementation project(':pickerview_library')

你可能感兴趣的