当前位置:   article > 正文

第三方开源库:日期时间选择器(TimePickerDialog)_timepicker开源

timepicker开源

TimePickerDialog

Android时间选择器,支持年月日时分,年月日,年月,月日时分,时分格式,可以设置最小时间和最大时间(精确到分)。
首先呢是效果图
这里写图片描述
其次呢是主要的代码块

public class MainActivity extends AppCompatActivity implements View.OnClickListener, OnDateSetListener {

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private Button btn7;
    private TextView tv;
    private TimePickerDialog dialogAll;

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

    private void initViews() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
    }

    private void findViews() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn6 = (Button) findViewById(R.id.btn6);
        btn7 = (Button) findViewById(R.id.btn7);
        tv = (TextView) findViewById(R.id.tv);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                //可自定义参数
                initAll();
                break;
            case R.id.btn2:
                dialogAll = new TimePickerDialog.Builder()
                        .setType(Type.ALL)
                        .setCallBack(this)
                        .build();
                dialogAll.show(getSupportFragmentManager(), "所有");
                break;
            case R.id.btn3:
                TimePickerDialog dialogYear = new TimePickerDialog.Builder()
                        .setType(Type.YEAR)
                        .setCallBack(this)
                        .build();
                dialogYear.show(getSupportFragmentManager(), "年");
                break;
            case R.id.btn4:
                TimePickerDialog dialogYearMonth = new TimePickerDialog.Builder()
                        .setType(Type.YEAR_MONTH)
                        .setCallBack(this)
                        .build();
                dialogYearMonth.show(getSupportFragmentManager(), "年_月");
                break;
            case R.id.btn5:
                TimePickerDialog dialogYearMonthDay = new TimePickerDialog.Builder()
                        .setType(Type.YEAR_MONTH_DAY)
                        .setCallBack(this)
                        .build();
                dialogYearMonthDay.show(getSupportFragmentManager(), "年_月_日");
                break;
            case R.id.btn6:
                TimePickerDialog dialogHourMins = new TimePickerDialog.Builder()
                        .setType(Type.HOURS_MINS)
                        .setCallBack(this)
                        .build();
                dialogHourMins.show(getSupportFragmentManager(), "时_分");
                break;
            case R.id.btn7:
                TimePickerDialog dialogMonthDayHourMin = new TimePickerDialog.Builder()
                        .setType(Type.MONTH_DAY_HOUR_MIN)
                        .setCallBack(this)
                        .build();
                dialogMonthDayHourMin.show(getSupportFragmentManager(), "月_日_时_分");
                break;
        }
    }

    private void initAll() {
        long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;
        TimePickerDialog mDialogAll = new TimePickerDialog.Builder()
                .setCallBack(this)
                .setCancelStringId("取消")
                .setSureStringId("确认")
                .setTitleStringId("时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
                .setHourText("时")
                .setMinuteText("分")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis())
                .setMaxMillseconds(System.currentTimeMillis() + tenYears)
                .setCurrentMillseconds(System.currentTimeMillis())
                .setThemeColor(getResources().getColor(R.color.timepicker_dialog_bg))
                .setType(Type.ALL)
                .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(getResources().getColor(R.color.timepicker_toolbar_bg))
                .setWheelItemTextSize(12)
                .build();

        mDialogAll.show(getSupportFragmentManager(), "ALL");
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        Date date = new Date(millseconds);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//精确到分钟
        String time = format.format(date);
        tv.setText("时间:" + time);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128

当然在使用的时候是引用的第三方的东西

    compile 'com.jzxiang.pickerview:TimePickerDialog:1.0.1'
  • 1

这样简单的效果就可以实现了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/628250
推荐阅读
相关标签
  

闽ICP备14008679号