Android Development _ Talk about Timepicker (time picker)

Source: Internet
Author: User

Timepicker also inherits from the Framelayout class. The time selection control shows the user the time of day (which can be 24 hours or am/pm) and allows the user to make a selection. If you want to capture events for user-modified time data, you need to add a Ontimechangedlistener listener for Timepicker

First, the method

public int Getbaseline ()

Returns the offset of the text baseline of the window space to its top boundary. If this part does not support baseline alignment, this method returns -1/.

return value

The offset of the baseline, which returns-1 if the baseline alignment is not supported.

Public Integer Getcurrenthour ()

Gets the hour portion of the current time.

return value

Current Hour (0-23)

Public Integer Getcurrentminute ()

Gets the minute portion of the current time.

return value

The current minute.

public boolean Is24hourview ()

Gets whether the current system setting is 24-hour.

return value

Returns true if it is a 24-hour system, otherwise false is returned.

public void Setcurrenthour (Integer currenthour)

Sets the current hour.

public void Setcurrentminute (Integer currentminute)

Sets the current minute (0-59).

public void SetEnabled (Boolean enabled)

Sets the available view state. The interpretation of the available view state is changed in the subclass.

Parameters

Enabled if available is true, otherwise false.

public void Setis24hourview (Boolean is24hourview)

The setting is 24 hours or morning/afternoon.

Parameters

Is24hourview true indicates a 24-hour system. False indicates the morning/afternoon system.

public void Setontimechangedlistener (Timepicker.ontimechangedlistener ontimechangedlistener)

Sets the callback function for the time adjustment event.

Parameters

Ontimechangedlistener callback function, cannot be empty.

Second, example (Global mode)

1. layout file

1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent"5android:orientation="Vertical">6 7<Timepicker8Android:id="@+id/id_timepicker1"9Android:layout_width="wrap_content"Tenandroid:layout_height="wrap_content"/> One  A</LinearLayout>

Second, activity

1 Package base_ui;2 3 import Java.util.Calendar;4 5 import COM.EXAMPLE.ALLCODE.R;6 7 import android.app.Activity;8 import Android.app.TimePickerDialog;9 import Android.app.TimePickerDialog.OnTimeSetListener;Ten import Android.os.Bundle; One import Android.text.format.Time; A import Android.widget.TimePicker; - import Android.widget.TimePicker.OnTimeChangedListener; -  the  Public classUi_timepicker extends activity{ -     PrivateTimepicker Timepicker; -     PrivateCalendar Cal;//Show Current Date -     Private intYear ; +     Private intmonth; -     Private intDay ; +     Private inthour; A     Private intminute; at      - @Override -     protected voidonCreate (Bundle savedinstancestate) { -          -         //TODO auto-generated Method Stub - super.oncreate (savedinstancestate); in Setcontentview (r.layout.ui_timepickerdialog); -Timepicker =(Timepicker) Findviewbyid (r.id.id_timepicker1); to      +         //get the object for the calendar -Cal=calendar.getinstance (); the                 //Get month and day time and minute information *Year = Cal.Get(calendar.year); $month = Cal.Get(Calendar.month) +1;//be careful, add one .Panax NotoginsengDay = Cal.Get(calendar.day_of_month); -hour = Cal.Get(calendar.hour_of_day); theminute = Cal.Get(Calendar.minute); +                  ASettitle ("Selected time:"+hour+"Time-"+minute+"points"); theTimepicker.setontimechangedlistener (NewOntimechangedlistener () { +               - @Override $               Public voidOntimechanged (Timepicker view,intHourintminute) { $                  //TODO auto-generated Method Stub -Settitle ("Selected time:"+hour+"Time-"+minute+"points"); -             } the          }); -         Wuyi          the          -     } Wu  -}

Iii. Examples (dialog box mode)

Corresponding to the above example 42--49 Line code

1 NewTimepickerdialog ( This,NewOntimesetlistener () {2             3 @Override4              Public voidOntimeset (Timepicker view,intHourintminute) {5                 //TODO auto-generated Method Stub6Settitle ("Selected time:"+hour+"Time-"+minute+"points");7             }8}, hour, minute,true). Show ();//The first two parameters are initialized, and the third parameter determines whether the 24-hour format is displayed9}

Android Development _ Talk about Timepicker (time picker)

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.