1. Mian.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:id=" @+id/ll " android:layout_width=" Fill_parent " android: Layout_height= "Fill_parent" android:orientation= "vertical" > <timepicker Android:id= "@+id/timepicker1" android:layout_width= "Match_parent" android: Layout_height= "Wrap_content" > </TimePicker></LinearLayout>
2.12 hours
Timepicker timepicker= (Timepicker) Findviewbyid (r.id.timepicker1); // Get Object Timepicker.setcurrenthour (8); // Set Hours Timepicker.setcurrentminute (n); // Set minutes
3.24 hours
Timepicker timepicker= (Timepicker) Findviewbyid (r.id.timepicker1); // Get Object Timepicker.setcurrenthour (a); // Set Hours Timepicker.setcurrentminute (n); // Set minutes Timepicker.setis24hourview (true); // set 24-hour display
4. Display time
Timepicker timepicker=(Timepicker) Findviewbyid (r.id.timepicker1); Timepicker.setcurrenthour (15); Timepicker.setcurrentminute (12); Timepicker.setis24hourview (true); Timepicker.setontimechangedlistener (NewTimepicker.ontimechangedlistener () {@Override Public voidOntimechanged (Timepicker view,intHourofday,intminute) { //TODO auto-generated Method StubToast.maketext (Getapplicationcontext (), "Current time:" +hourofday+ "point" +minute+ "min", Toast.length_long). Show (); //Show Time Information } });
5. Determine if the 24-hour system is displayed according to the results
Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" Fill_parent " Android:layout_height= "Fill_parent" android:id= "@+id/ll" > <timepicker Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:id= "@+id/timePicker1" > </TimePicker> <button android:layout_width= "Match_parent" Android:layout_ height= "Wrap_content" android:id= "@+id/button1" android:text= "@string/btn1" > </ Button></linearlayout>
Firstactivity
Button bt1= (button) Findviewbyid (R.id.button1);//Get Button Object FinalTimepicker timepicker= (Timepicker) Findviewbyid (r.id.timepicker1);//Get Timepicker ObjectTimepicker.setcurrenthour (15);//Set HoursTimepicker.setcurrentminute (12);//Set minutesBt1.setonclicklistener (NewView.onclicklistener () {//Setting the button listener@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub intHour,minite; Booleanis24v; Hour=timepicker.getcurrenthour ();//Get display hoursMinite=timepicker.getcurrentminute ();//Get display minutesIs24v=timepicker.is24hourview ();//gets whether the 24-hour system if(is24v)//24-hour system situation{Toast.maketext (Getapplicationcontext (),"Current time:" +hour+ "hours" +minite+ "minutes", Toast.length_long). Show (); //Show Time Information } Else //Morning/Afternoon mode { if(hour<12) {Toast.maketext (Getapplicationcontext (),"Current time: AM" +hour+ "hours" +minite+ "minutes", Toast.length_long). Show (); //Show Time Information } Else{Toast.maketext (Getapplicationcontext (),"Current Time: PM" + (hour-12) + "Hour" +minite+ "minutes", Toast.length_long). Show (); //Show Time Information } } } });
Android--Use of Time controls