Beginner Android, wrote a modified time of the small program, the implementation of the following;
Click the Change button to pop up the time dialog box;
Then click Done to set the display time;
There are two ways to achieve this,
One is the use of the ShowDialog method, but this method has expired, but can also be used;
The other is to use the Dialogfragment class to achieve;
First look at the second way:
Where layout's activity_main.xml is configured as:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <TextViewAndroid:id= "@+id/timepicker_display"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:gravity= "Center"android:textsize= "20SP" /> <ButtonAndroid:id= "@+id/button_timepicker"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Change" /></LinearLayout>
Mainactivity.java:
PackageCom.process.processbar;ImportJava.util.Calendar;Importandroid.app.Activity;ImportAndroid.app.Dialog;Importandroid.app.DialogFragment;ImportAndroid.app.TimePickerDialog;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.TimePicker; Public classMainactivityextendsActivity {PrivateTextView Timepickerdisplay; PrivateButton Buttontimepicker; Private intMhour;//Set Hours Private intMminute;//Set minutes Static Final inttime_dialog_id = 0;//the ID of the dialog box@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Timepickerdisplay=(TextView) Findviewbyid (R.id.timepicker_display); Buttontimepicker=(Button) Findviewbyid (R.id.button_timepicker); Buttontimepicker.setonclicklistener (listener); //get the current time of the emulator through the calendar FinalCalendar C =calendar.getinstance (); Mhour=C.get (Calendar.hour_of_day); Mminute=C.get (Calendar.minute); //displays the time of the current emulatorTimepickerdisplay.settext (NewStringBuilder (). Append (Pad (mhour)). Append (":"). Append (Pad (mminute))); } PrivateOnclicklistener listener =NewOnclicklistener () { Public voidOnClick (View v) {dialogfragment fragment=Newmydialogfragment (); Fragment.show (Getfragmentmanager (),"DatePicker"); } }; //Set display format Private StaticString pad (intc) {return(c >= 10)? String.valueof (c): "0" +string.valueof (c); } //use inner classes to implement classMydialogfragmentextendsdialogfragment { PublicDialog Oncreatedialog (intID) {Switch(ID) { Casetime_dialog_id:return NewTimepickerdialog (Getactivity (), Mtimesetlistener, Mhour, Mminute,true); } return NULL; } @Override PublicDialog Oncreatedialog (Bundle savedinstancestate) {return NewTimepickerdialog (Getactivity (), Mtimesetlistener, Mhour, Mminute,true); } PrivateTimepickerdialog.ontimesetlistener Mtimesetlistener =NewTimepickerdialog.ontimesetlistener () { Public voidOntimeset (Timepicker view,intHourofday,intminute) {timepickerdisplay. SetText (NewStringBuilder (). Append (Pad (hourofday)). Append (":"). Append (Pad (minute))); } }; }}
The first way is:
Mainactivity.java:
PackageCom.process.processbar;ImportJava.util.Calendar;Importandroid.app.Activity;ImportAndroid.app.Dialog;ImportAndroid.app.TimePickerDialog;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.TimePicker; Public classMainactivityextendsActivity {PrivateTextView Timepickerdisplay; PrivateButton Buttontimepicker; Private intMhour;//Set Hours Private intMminute;//Set minutes Static Final inttime_dialog_id = 0;//the ID of the dialog box@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Timepickerdisplay=(TextView) Findviewbyid (R.id.timepicker_display); Buttontimepicker=(Button) Findviewbyid (R.id.button_timepicker); Buttontimepicker.setonclicklistener (listener); //get the current time of the emulator through the calendar FinalCalendar C =calendar.getinstance (); Mhour=C.get (Calendar.hour_of_day); Mminute=C.get (Calendar.minute); //displays the time of the current emulatorTimepickerdisplay.settext (NewStringBuilder (). Append (Pad (mhour)). Append (":"). Append (Pad (mminute))); } PrivateOnclicklistener listener =NewOnclicklistener () { Public voidOnClick (View v) {showDialog (time_dialog_id);//method has expired } }; //Set display time Private StaticString pad (intc) {return(c >= 10)? String.valueof (c): "0" +string.valueof (c); } //Listener PrivateTimepickerdialog.ontimesetlistener Mtimesetlistener =NewTimepickerdialog.ontimesetlistener () { Public voidOntimeset (Timepicker view,intHourofday,intminute) {Timepickerdisplay.settext (NewStringBuilder (). Append (Pad (hourofday)). Append (":"). Append (Pad (minute))); } }; //when calling ShowDialog, the Oncreatedialog method is called, and we need to override this method to create a dialog in the method;@OverrideprotectedDialog Oncreatedialog (intID) {Switch(ID) { Casetime_dialog_id:return NewTimepickerdialog ( This, Mtimesetlistener, Mhour, Mminute,true); } return NULL; }}
Note: The ShowDialog method has expired, although it can be used, but the official proposed to use the second way to replace the ShowDialog;
Android Modified by timepickerdialog time