1、在布局文件中添加TimePicker:
<TimePicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/timepicker" > </TimePicker>
2、java代码:
通过id获取布局中的TimePicker:
TimePicker timePicker=findViewById(R.id.timepicker);
设置TimePicker的点击事件:onTimeChanged第一个参数是时,第二个参数是分
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker timePicker, int i, int i1) { String str=i+"时"+i1+"分"; Toast.makeText(TimepickerActivity.this,str,Toast.LENGTH_LONG).show(); } });
若要显示24小时制,添加以下方法:
timePicker.setI