Home>

I believe that everyone is no stranger to setting the time and date of mobile phones.Let ’s take an example of setting the date and time today.There are some imperfections,For example, how to synchronize the set time and date with the mobile phone system.Interested readers can improve based on their own experience.

The first is the time setting:

The .java file (mainactivity.java) code is as follows:

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 mybutton1=null;
 private button mybutton2=null;
 private int mhour;
 private int mminute;
 static final int time_dialog_id=0;
 private timepickerdialog.ontimesetlistener mtimesetlistener=new timepickerdialog.ontimesetlistener () {
  @override
  public void ontimeset (timepicker view, int hourofday, int minute) {
   // todo auto-generated method stub
   mhour=hourofday;
   mminute=minute;
   updatedisplay ();
  }
 };
 @override
 protected 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 () {
   @override
   public void onclick (view v) {
    // todo auto-generated method stub
    showdialog (time_dialog_id);
   }
  });
  mybutton2.setonclicklistener (new onclicklistener () {
   @override
   public void onclick (view v) {
    // todo auto-generated method stub
    mainactivity.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>= 10)
   return string.valueof (i);
  else
   return "0" + string.valueof (i);
 }
 @override
 protected dialog oncreatedialog (int id) {
  switch (id) {
  case time_dialog_id:
   return new timepickerdialog (this, mtimesetlistener, mhour, mminute,     false);
  }
  return null;
 }
}

The layout file (activity_main.xml) code is as follows:

<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"
  />
</linearlayout>
</relativelayout>

The effect is shown in the following figure:

The date and time settings are basically the same,I will not repeat them here.The reader can debug and improve the sample code in this article,I believe there will be new gains!

  • Previous The Road to iOS Development-Weibo New Features Page
  • Next A case caused by an equal sign (talking about the correct 404 configuration of Nginx)