A07_TimePicker DatePicker AnalogClock DigitalClock setting Summary

Source: Internet
Author: User

Objective: To set the learning time, date, and clock
Picker's computer major explanation is "selector ".
Simple translation:

TimePicker time Selector
DatePicker date Selector
AnalogClock analog clock
DigitalClock digital clock

I. TimePicker
1. The Listener interface used by TimePicker is OnTimeChangedListener.
2. TimePicker displays the current system time by default. You can use setCurrentHour and setCurrentMinute to set the default display time.
3. You can use setIs24HourView to set TimePicker to be displayed in 24-hour format.
4. Obtain the current TimePicker time. Use the getCurrentHour and getCurrentMinute methods.
Simulator android4.2 display effect (not in 24-hour format ):

Real machine android2.3.7 display effect (not in 24-hour format ):

Real machine android2.3.7 display effect (in 24-hour format ):

Java code:Copy codeThe Code is 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 );
// Create a listener for timePicker
TimePickerListener timeListener = new TimePickerListener ();
TimePicker. setOnTimeChangedListener (timeListener );
// Create a listener for the button
ButtonListener buttonListener = new ButtonListener ();
Button. setOnClickListener (buttonListener );
// TimePicker displays the current time by default. You can manually specify the default display time.
TimePicker. setCurrentHour (12 );
TimePicker. setCurrentMinute (0 );
// Set the display format to 24-hour
TimePicker. setIs24HourView (true );
}
Class TimePickerListener implements OnTimeChangedListener {
Public void onTimeChanged (TimePicker view, int hourOfDay, int minute ){
// Use Toast to display the TimePicker time
String time = hourOfDay + "point:" + minute + "point ";
Toast. makeText (MainActivity. this, time, Toast. LENGTH_SHORT). show ();
}
}
Class ButtonListener implements OnClickListener {
Public void onClick (View v ){
String time = timePicker. getCurrentHour () + "point :"
+ TimePicker. getCurrentMinute () + "Minute ";
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 codeThe 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"
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 = "getting the set time"
Android: layout_below = "@ id/timePickerId"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "50dp"/>
</RelativeLayout>

2. DatePicker
1. DatePicker does not have a listener interface similar to OnTimeChangedListener like TimePicker. There is a dialog box that will be added later.
For more information, see setting the DatePicker dialog box.
Simulator android4.2:

Mobile android2.3.7:

Java code:Copy codeThe Code is 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 a listener for the 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 codeThe 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"
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>

Iii. AnalogClock

The displayed clock time varies with the system time.
The code is relatively simple, so you don't need to paste it. You just need to add an <AnalogClock/> label to the xml layout file.
Simulator android4.2:

Mobile android2.3.7:

Iv. DigitalClock

The displayed clock time varies with the system time.
Simulator android4.2:

Mobile android2.3.7:

Xml code:Copy codeThe 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"
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 specific operation will be used later ~

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.