For the time and date setting of the mobile phone you must be not unfamiliar with it, today do a small example of time and date settings, which encountered a problem, guidance, how to make the set date and the phone system synchronization? Also hope master pointing twos.
Let's not talk about it, share my little examples and see if it will help you.
The first is the time setting:
. java file (Mainactivity.java):
Package Com.example.activity_time_date;import Java.util.calendar;import Android.app.activity;import Android.app.dialog;import Android.app.timepickerdialog;import Android.content.intent;import Android.os.Bundle; Import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.textview;import Android.widget.timepicker;public class Mainactivity extends Activity {private TextView mytext = null;private Button mybut Ton1 = null;private Button mybutton2 = null;private int mhour;private int mminute;static final int time_dialog_id = 0;priv Ate Timepickerdialog.ontimesetlistener Mtimesetlistener = new Timepickerdialog.ontimesetlistener () {@Overridepublic void Ontimeset (timepicker view, int hourofday, int minute) {//TODO auto-generated Method stubmhour = Hourofday;mminute = Minute;updatedisplay ();}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);Setcontentview (r.layout.activity_main); mytext = (TextView) Findviewbyid (r.id.textview); mybutton1 = (Button) Findviewbyid (r.id.button1); mybutton2 = (Button) Findviewbyid (R.id.button2); Mybutton1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubshowdialog (time_dialog_id);}}); Mybutton2.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method StubMainActivity.this.startActivity (New Intent (Mainactivity.this, Dateactivity.class));}); Final Calendar cal = Calendar.getinstance (); mhour = Cal.get (calendar.hour_of_day); mminute = Cal.get (Calendar.minute); Updatedisplay ();} private void Updatedisplay () {Mytext.settext (New StringBuilder (). Append (Pad (mhour)). Append (":"). Append (Pad (mminute )));} private static String pad (int i) {if (I >=) return string.valueof (i); Elsereturn "0" + string.valueof (i);} @Overrideprotected Dialog oncreatedialog (int id) {switch (ID) {Case time_dialog_id:return NEW Timepickerdialog (This, Mtimesetlistener, Mhour, mminute,false);} return null;}}
Layout file (activity_main.xml):
<relativelayout 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 " tools:context= "${relativepackage}.${activityclass}" ><linearlayout android:layout_width= "Fill_ Parent " android:layout_height=" fill_parent " android:orientation=" vertical " > <textview android:id= "@+id/textview" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content " /><button android:id=" @+id/button1 " android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:text= "Set time" /><button android:id= "@+id/ Button2 " android:layout_width=" fill_parent " android:layout_height=" wrap_content " android:text=" Next Page " /></linearlayout></relativelayout>
Date settings and time settings are basically the same; do not repeat here, if you need source code please leave a message contact.
Android Development Time date 1