Home>

Time and date setting components:timepicker, datepicker

Can be directly added to our layout style in the layout file,The specific code is as follows:

<linearlayout
    android:id="@ + id/linear1"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
  <edittext
    android:id="@ + id/editview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <linearlayout
    android:id="@ + id/linear2"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    <<br>// time setting component
    <timepicker
      android:id="@ + id/time"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      /><br>// date setting component
    <datepicker
      android:id="@ + id/date"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />
  </linearlayout>
</linearlayout>

The .java file code is as follows:

public class mainactivity extends activity {
  private edittext myedit=null;<br>// Declaring two components
  private timepicker time=null;
  private datepicker date=null;
  @override
  protected void oncreate (bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    setcontentview (r.layout.activity_main);<br><br>
    setrequestedorientation (activityinfo.screen_orientation_landscape);
    myedit=(edittext) findviewbyid (r.id.editview);<br>// Call the two components in the layout file
    time=(timepicker) findviewbyid (r.id.time);
    date=(datepicker) findviewbyid (r.id.date);
    //time.setis24hourview(true);//Set the time display in 24-hour format,System default 12-hour clock
    time.setontimechangedlistener (new mytime ());<br>// Note here that the date has no ondatechangedlistener () method, we set it here by calling the date's init () method.
    date.init (this.date.getyear (), this.date.getmonth (), this.date.getdayofmonth (), new mydate ());
    this.setdatetime ();
  }
// Time change event listener:<br><br>class mytime implements ontimechangedlistener {
    @override
    public void ontimechanged (timepicker view, int hourofday, int minute) {
      // todo auto-generated method stub
      mainactivity.this.setdatetime ();
    }
  }
  <br>// date change event listener
  @suppresslint ("newapi")
  class mydate implements ondatechangedlistener {
    @override
    public void ondatechanged (datepicker view, int year, int monthofyear,        int dayofmonth) {
      // todo auto-generated method stub
      mainactivity.this.setdatetime ();
    }
  }
  public void setdatetime () {// The content of the text input component must be modified due to the date and time change
    this.myedit.settext (this.date.getyear () + "-"
        + (this.date.getmonth () + 1) + "-" + this.date.getdayofmonth ()
        + "" + this.time.getcurrenthour () + ":"
        + this.time.getcurrentminute ());// modify the content of the text
  }
}

The more difficult point in this example is thatThe date event does not have an changedlistener () method. Here we use the date's init () method instead.The second is how to control the display direction of the activity,The display direction of the activity is the vertical display by default.We can re-set it through the java file,It can also be set in the configuration file,This is where the settings in the java file are more flexible.

  • Previous A case caused by an equal sign (talking about the correct 404 configuration of Nginx)
  • Next Getting to know Java arrays