Basic android control learning ----- Date & amp; Time, android ----- date

Source: Internet
Author: User

Basic android control learning ----- Date & Time, android ----- date

Date & Time:

TextClock (Text clock), AnalogClock (analog clock), Chronometer (timer), DatePicker (date selector), TimePicker (Time selector), and CalendarView (date view) the first three of these six are rarely used. If we need to customize the last three controls in actual applications, we do not think they are used much, you can start to see how to run it.

I. TextClock (Text clock) is a new control launched after android4.2 (API17) to replace a previous Digital control, textClock can be used to display the current time and date in string format. The time control provides two formats: 12-hour and 24-hour. The following describes the methods used:

Is24HourModeEnabled (): Check whether the system displays the time in the 24-hour format.

Android: format24Hour: Set the 24-hour format

Android: format12Hour: Set the 12-hour format

Android: timeZone: set the time zone

Usage:

<TextClock        android:layout_width="match_parent"        android:layout_height="40dp"        android:format24Hour="yyyy : MM :DD"/>

2. AnalogClock (analog clock)
Summary of several attributes

Android: dial: background image of the table

Android: hand_hour: the hour-hand image of the table

Android: hand_minute: minute-level image of the table

You can try these three attributes by yourself.

3. Chronometer)

Let's look at an example.

Code:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <Chronometer android: id = "@ + id/chronometer" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center" android: textColor = "# ff0000" android: textSize = "60dip"/> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_margin = "10dip" android: orientation = "horizontal"> <Button android: id = "@ + id/btnStart" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "Start recording"/> <Button android: id = "@ + id/btnStop" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "stop recording"/> <Button android: id = "@ + id/btnReset" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1" android: text = "reset"/> <Button android: id = "@ + id/btn_format" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "formatting"/> </LinearLayout>

Java code

Package com. example. test3; import android. app. activity; import android. OS. bundle; import android. OS. systemClock; import android. view. view; import android. widget. button; import android. widget. chronometer; import android. widget. toast; public class MainActivity extends Activity implements View. onClickListener, Chronometer. onChronometerTickListener {private Chronometer chronometer; private Button btn_start, btn _ Stop, btn_base, btn_format; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView ();} private void initView () {chronometer = (Chronometer) findViewById (R. id. chronometer); btn_start = (Button) findViewById (R. id. btnStart); btn_stop = (Button) findViewById (R. id. btnStop); btn_base = (Button) findViewById (R. id. BtnReset); btn_format = (Button) findViewById (R. id. btn_format); chronometer. listener (this); btn_start.setOnClickListener (this); listener (this); btn_format.setOnClickListener (this);} @ Override public void onClick (View v) {switch (v. getId () {case R. id. btnStart: chronometer. start (); // start timing break; case R. id. btnStop: chronome Ter. stop (); // stop timing break; case R. id. btnReset: chronometer. setBase (SystemClock. elapsedRealtime (); // reset break; case R. id. btn_format: chronometer. setFormat ("Time: % s"); // change the time display format break; }}@ Override public void onChronometerTick (Chronometer chronometer) {String Time = chronometer. getText (). toString (); if (time. equals ("00:00") {Toast. makeText (MainActivity. this, "the time is up ~ ", Toast. LENGTH_SHORT). show ();}}}

4. DatePicker (date selector)

Core Attributes: Let's look at the figure above. If you are interested, try it on your own.

Android: calendarTextColor: The color of the calendar list

Android: calendarViewShown: whether to display the calendar view, that is, the right part

Android: datePickerMode: component appearance. Optional values: spinner and calendar. You can try it on your own.

Android: dayOfWeekBackground: background color of the day of the week on the top

Android: dayOfWeekTextAppearance: text color of the day of the week on the top

Android: endYear last year

Android: firstDayOfWeek

Android: headerBackground: background color of the entire header

Android: headerDayOfMonthTextAppearance: Color of the header date font android: headerMonthTextAppearance: font color of the header month

Android: headerYearTextAppearance: font color of the header year

Android: maxDate: The maximum date is displayed in mm/dd/yyyy format in this calendar view.

Android: maxDate: The minimum date is displayed in mm/dd/yyyy format in this calendar view. android: spinnersShown: whether to display the spinner

Android: startYear: set the first year (content). For example, android: yearListItemTextAppearance: The list text appears in the list. Android: yearListSelectorColor: year list selected color

Listener event OnDateChangedListener

5. TimePicker (Time selector)

The attributes are similar to the previous ones. Forget it. Listen to the OnTimeChangedListener event.

Vi. calendar View)

The Calendar has an important kind of Calendar when learning j2SE, which is almost useless. You can try these controls on your own. There is nothing to summarize.

Android: firstDayOfWeek: set the first day of a week.

Android: maxDate: The maximum date is displayed in mm/dd/yyyy format android: minDate: The minimum date is displayed in mm/dd/yyyy format android: weekDayTextAppearance: the text of the workday appears in the abbreviation of the calendar title

The corresponding date change event is: CalendarView. OnDateChangeListener

Summary: The basic UI components of android are almost the same, and some UI components related to Adapter will be summarized later.

Related Article

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.