A07_timepicker & DatePicker & analogclock & DigitalClock Setup Summary _android

Source: Internet
Author: User
Tags set time
Goal: Learning time dates and clock settings
Picker's computer professional explanation is "selector".
Simple translation:

Timepicker Time Picker
DatePicker Date Picker
AnalogClock Analog Clock
DigitalClock Digital Clock

First, Timepicker
The listener interface used by 1.TimePicker is Ontimechangedlistener
2.TimePicker The default display system current time, you can use the Setcurrenthour and Setcurrentminute two methods to set the default display time
3. You can use the Setis24hourview method to set Timepicker to display in 24-hour system
4. Get Timepicker current time, use Getcurrenthour and Getcurrentminute two methods
Simulator android4.2 Display effect (non 24 hour):

True machine android2.3.7 Display effect (not 24 hours):

True machine android2.3.7 Display effect (24-hour system):

Java code:
Copy Code code as follows:

Package com.haut.a07_timepicker;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.TimePicker;
Import Android.widget.TimePicker.OnTimeChangedListener;
Import Android.widget.Toast;
public class Mainactivity extends activity {
Private Timepicker Timepicker;
Private button button;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Timepicker = (timepicker) Findviewbyid (R.id.timepickerid);
Button = (button) Findviewbyid (R.id.buttonid);
Creating listeners for Timepicker
Timepickerlistener TimeListener = new Timepickerlistener ();
Timepicker.setontimechangedlistener (TimeListener);
Create Listener for button
Buttonlistener Buttonlistener = new Buttonlistener ();
Button.setonclicklistener (Buttonlistener);
Timepicker Displays the current time by default and can manually set its default display time
Timepicker.setcurrenthour (12);
Timepicker.setcurrentminute (0);
Set display format to 24-hour system
Timepicker.setis24hourview (TRUE);
}
Class Timepickerlistener implements Ontimechangedlistener {
public void ontimechanged (timepicker view, int hourofday, int minute) {
Use Toast to display Timepicker time
String time = Hourofday + "point:" + minute + "min";
Toast.maketext (Mainactivity.this, Time, Toast.length_short). Show ();
}
}
Class Buttonlistener implements Onclicklistener {
public void OnClick (View v) {
String time = Timepicker.getcurrenthour () + "dot:"
+ timepicker.getcurrentminute () + "min";
Toast.maketext (Mainactivity.this, Time, Toast.length_short). Show ();
}
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}

XML code:
Copy Code code 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"
android:background= "@drawable/folwer1"
Tools:context= ". Mainactivity ">
<timepicker
Android:id= "@+id/timepickerid"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centerhorizontal= "true"/>
<button
Android:id= "@+id/buttonid"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Get Set Time"
android:layout_below= "@id/timepickerid"
Android:layout_centerhorizontal= "true"
android:layout_margintop= "50DP"/>
</RelativeLayout>

Second, DatePicker
1.DatePicker does not have a listener interface similar to Ontimechangedlistener as Timepicker. There is a dialog box to add later.
Add: DatePicker dialog box settings
Simulator android4.2 Effect Diagram:

Mobile android2.3.7 Effect Chart:

Java code:
Copy Code code as follows:

Package com.haut.a07_datepicker;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.DatePicker;
Import Android.widget.Toast;
public class Mainactivity extends activity {
Private DatePicker DatePicker;
Private button button;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
DatePicker = (DatePicker) Findviewbyid (R.id.datepickerid);
Button = (button) Findviewbyid (R.id.buttonid);
Create Listener for button
Buttonlistener Buttonlistener = new Buttonlistener ();
Button.setonclicklistener (Buttonlistener);
}
Class Buttonlistener implements onclicklistener{
public void OnClick (View v) {
String date = datepicker.getyear () + "year:" + datepicker.getmonth () + "Month:" + datepicker.getdayofmonth () + "Day";
Toast.maketext (mainactivity.this, date, Toast.length_short). Show ();
}
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}

XML code:
Copy Code code 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"
android:background= "@drawable/leaf"
Tools:context= ". Mainactivity ">
<datepicker
Android:id= "@+id/datepickerid"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centerhorizontal= "true"/>
<button
Android:id= "@+id/buttonid"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Get Set Date"
android:layout_below= "@id/datepickerid"
Android:layout_centerhorizontal= "true"
android:layout_margintop= "50DP"/>
</RelativeLayout>

Third, AnalogClock

The clock time that is displayed changes as the system time changes.
The code is simpler and does not stick, just add a <AnalogClock/> tag to the XML layout file.
Simulator android4.2 Effect Diagram:

Mobile android2.3.7 Effect Chart:

Four, DigitalClock

The clock time that is displayed changes as the system time changes.
Simulator android4.2 Effect Diagram:

Mobile android2.3.7 Effect Chart:

XML code:
Copy Code code 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"
android:background= "@drawable/folwer"
Tools:context= ". Mainactivity ">
<digitalclock
Android:id= "@+id/digitalclockid"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centerhorizontal= "true"
android:layout_margintop= "100DP"
Android:textcolor= "#ff0000"
Android:textsize= "30sp"/>
</RelativeLayout>

The concrete operation is later used to replenish the concrete ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.