布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layout_spinner"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 这里显示方式是一个两个滚动条,里面一列是时,一条列分-->
<TimePicker
android:id="@+id/time_piker_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:timePickerMode="spinner">
</TimePicker>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_clock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<!-- 这里显示方式是一个时钟,第一次点击是选择时,第二次点击是选择分-->
<TimePicker
android:id="@+id/time_picker_clock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
</TimePicker>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 点击它改变显示的TimePicker-->
<Button
android:id="@+id/change_timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="change"
android:layout_gravity="center"/>
<!-- 点击它输出TimePicker中的时间-->
<Button
android:id="@+id/setdata_timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="set data"
android:layout_gravity="center"/>
</LinearLayout>
</RelativeLayout>
Java文件:
public class TimePickActivity extends Activity implements View.OnClickListener{
private TimePicker timePicker_spinner;
private TimePicker timePicker_clock;
private Button btn_change;
private Button btn_setData;
private LinearLayout layout_clock;
private LinearLayout layout_spinner;
private boolean change_Timepicker = false;
private int clock_hour;
private int clock_minute;
private int spinner_hour;
private int spinner_minute;
private String clock;
private String spinner;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timepick_layout);
layout_clock = findViewById(R.id.layout_clock);
layout_spinner = findViewById(R.id.layout_spinner);
btn_change = findViewById(R.id.change_timepicker);
btn_setData = findViewById(R.id.setdata_timepicker);
timePicker_spinner = findViewById(R.id.time_piker_spinner);
timePicker_clock = findViewById(R.id.time_picker_clock);
btn_change.setOnClickListener(this);
btn_setData.setOnClickListener(this);
//设置其显示方式为24小时(如果参数为false则分为AM与PM显示)
timePicker_clock.setIs24HourView(true);
timePicker_spinner.setIs24HourView(true);
//自动获取焦点
timePicker_spinner.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
timePicker_clock.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
//设置初始值(即00:00)
timePicker_clock.setCurrentHour(0);
timePicker_clock.setCurrentMinute(0);
timePicker_spinner.setCurrentHour(0);
timePicker_spinner.setCurrentMinute(0);
//通过OnTimeChangedListener来得到当前的时与分
timePicker_spinner.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
spinner_hour = hourOfDay;
spinner_minute = minute;
}
});
timePicker_clock.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
clock_hour = hourOfDay;
clock_minute = minute;
}
});
}
@Override
public void onClick(View v) {
switch(v.getId()){
//使用该按钮更换两种形式Clock Spinner
case R.id.change_timepicker:
if(!change_Timepicker){
layout_spinner.setVisibility(View.GONE);
layout_clock.setVisibility(View.VISIBLE);
change_Timepicker = !change_Timepicker;
}else{
layout_spinner.setVisibility(View.VISIBLE);
layout_clock.setVisibility(View.GONE);
change_Timepicker = !change_Timepicker;
}
break;
//得到我们所需求的时间
case R.id.setdata_timepicker:
if(!change_Timepicker){
spinner = getTimeString(spinner_hour,spinner_minute);
Toast.makeText(TimePickActivity.this, spinner, Toast.LENGTH_SHORT).show();
}else{
clock = getTimeString(clock_hour,clock_minute);
Toast.makeText(TimePickActivity.this, clock, Toast.LENGTH_SHORT).show();
}
}
}
public String getTimeString(int hour, int minute){
String string_hour, string_minute;
// 显示格式:xx:xx
// for example:03:07
if(hour < 10){
string_hour = "0" + hour;
}else{
string_hour = "" + hour;
}
if(minute < 10){
string_minute = "0" + minute;
}else {
string_minute = "" + minute;
}
return string_hour + ":" + string_minute;
}
}
转自:https://blog.csdn.net/qq_39079912/article/details/79696399