<1.XML布局文件>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TimePicker
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/time"/>
</LinearLayout>
<2.源代码>
public class MainActivity extends AppCompatActivity {
private TimePicker timePicker;
private int hour,min;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化控件
timePicker = (TimePicker) findViewById(R.id.time);
//获取当前小时
hour = Calendar.getInstance().get(Calendar.HOUR);
//获取当前分钟
min = Calendar.getInstance().get(Calendar.MINUTE);
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, "您选择的时间是"+hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();
}
});
}
}
PS:因为手机类型不同,所以在各个手机显示的样式也会不同