赞
踩
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);
}
}
当然在使用的时候是引用的第三方的东西
compile 'com.jzxiang.pickerview:TimePickerDialog:1.0.1'
这样简单的效果就可以实现了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。