Following the example of the previous time and date setup, today I want to introduce the components of the Android layout component with time and date settings in the hope of helping. Specifically as follows:
Time Date Setup component: Timepicker, DatePicker
In the layout file, you can add it directly to our layout style, as shown in the following code:
<linearlayout
android:id= "@+id/linear1"
android:orientation= "vertical"
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 Setup Component
<timepicker
android:id= "@+ Id/time "
android:layout_width=" wrap_content "
android:layout_height=" wrap_content "
/> " <br>//Date set 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>//Declaration of two components private timepic
Ker time = null;
Private DatePicker date = null;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);<br><br>//Set the display direction of the activity to horizontal screen setrequestedorientation (Activ
Ityinfo.screen_orientation_landscape); Myedit = (edittext) Findviewbyid (R.id.editview);<br>//Call two components in the layout file time = (Timepicker) Findviewbyid (r.id.
Time);
Date = (DatePicker) Findviewbyid (r.id.date); Time.setis24hourview (TRUE)//set to use 24-hour time display, system default 12-hour Time.setontimechangedlistener (new MyTime ());<br>
The note here is that the date does not have the Ondatechangedlistener () method, which we set here by the Init () method of the call date.
Date.init (This.date.getYear (), This.date.getMonth (), This.date.getDayOfMonth (), New MyDate ());
This.setdatetime (); //Time Change Event Monitor: <br><br> class MytIME implements ontimechangedlistener{@Override public void ontimechanged (timepicker view, int hourofday, int minu
TE) {//TODO auto-generated Method Stub MainActivity.this.setDateTime (); <br>//Date Change Event listener @SuppressLint ("Newapi") class MyDate implements ondatechangedlistener{@Overr IDE public void ondatechanged (DatePicker view, int year, int monthofyear, int dayofmonth) {//TODO Auto
-generated method Stub MainActivity.this.setDateTime ();
} public void SetDateTime () {//the contents of the text input component will also be modified after the date time change This.myedit.setText (this.date.getYear () + "-"
+ (This.date.getMonth () + 1) + "-" + this.date.getDayOfMonth () + "" + this.time.getCurrentHour () + ":" + This.time.getCurrentMinute ()); Modify the contents of the text}}
The effect chart is as follows:
The difficult point in this example is that there is no Onchangedlistener () method for the date event, where we use the date init () method instead. The second is how to control the direction of activity display, activity display direction by default is vertical display, we can through the Java file from the new settings, can also be set in the configuration file, which is the Java file set more flexible place.