Android Timepicker and DatePicker time-date selection component usages _android

Source: Internet
Author: User
Tags stub

The effects and the code are very intuitive:

Instance 1:timepicker

 <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:paddingbottom=" @dimen/activity_vertical_margin "android:paddingleft=" @dimen/activity_horizontal_ Margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_vertical_ Margin "tools:context=". 
    Mainactivity "> <timepicker android:id=" @+id/timepic1 "android:layout_height=" Wrap_content " Android:layout_width= "Match_parent"/> <button android:id= "@+id/buttone1" android:layout_width= H_parent "android:layout_height=" wrap_content "android:layout_below=" @id/timepic1 "android:text=" Get Timepick Time "/> </RelativeLayout> 
Package com.android.xiong.times; 
Import Android.os.Bundle; 
Import android.app.Activity; 
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; 
  public class Mainactivity extends activity {private Timepicker timePick1; 
 
  Private Button buttone1; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.activity_main); 
     
    timepick1= (Timepicker) Findviewbyid (R.ID.TIMEPIC1); 
    buttone1= (Button) Findviewbyid (R.ID.BUTTONE1); 
    Onchangelistener buc=new Onchangelistener (); 
    Buttone1.setonclicklistener (BUC); 
    Whether to use the 24-hour Timepick1.setis24hourview (true); 
    TimeListener times=new TimeListener (); 
  Timepick1.setontimechangedlistener (times); 
Class Onchangelistener implements onclicklistener{    @Override public void OnClick (View v) {//TODO auto-generated method stub int H=timepick1.getcurre 
      Nthour (); 
      int M=timepick1.getcurrentminute (); 
    System.out.println ("H:" +h+ "M:" +m); The class TimeListener implements ontimechangedlistener{/** * View is currently selected Timepicker control * hourof Day the current control selects the Timepicker hour * minute the minute of the currently selected control Timepicker/@Override public void ontimechanged (Timepi Cker view, int hourofday, int minute) {//TODO auto-generated Method Stub System.out.println ("H:" + HOUROFDA 
    Y + "M:" +minute); }} @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds items to 
    The Action Bar if it is present. 
    Getmenuinflater (). Inflate (R.menu.main, menu); 
  return true; 

 } 
 
}

Instance 2:datepicker

<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:paddingbottom= "@dimen/activity_vertical_margin" 
  android:paddingleft= "@dimen/activity_ Horizontal_margin " 
  android:paddingright=" @dimen/activity_horizontal_margin " 
  android:paddingtop=" @dimen /activity_vertical_margin " 
  tools:context=". Mainactivity "> 
 
  <datepicker  
    android:id=" @+id/datepick1 " 
    android:layout_height=" Wrap_ Content " 
    android:layout_width=" match_parent " 
    /> 
  <button  
    android:id=" @+id/button1 " 
    android:layout_below= "@id/datepick1" 
    android:layout_width= "match_parent" 
    android:layout_height = "Wrap_content" 
    android:text= "get datepicker value"/> 
 
</RelativeLayout> 

Package com.android.xiong.datepicker; 
Import Android.os.Bundle; 
Import android.app.Activity; 
Import Android.view.Menu; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
 
Import Android.widget.DatePicker; 
  public class Mainactivity extends activity {private DatePicker datePicker1; 
  Private Button button1; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.activity_main); 
    datepicker1= (DatePicker) Findviewbyid (R.ID.DATEPICK1); 
    Set the default time like September 9, 2055 Datepicker1.updatedate (2012, 8, 9); 
    button1= (Button) Findviewbyid (R.id.button1); 
    Oncliclisers cl=new oncliclisers (); 
  Button1.setonclicklistener (CL); Class Oncliclisers implements onclicklistener{@Override public void OnClick (View v) {//TODO Aut 
      o-generated method Stub int y=datepicker1.getyear (); int M=datepicker1.getmontH () +1; 
      int D=datepicker1.getdayofmonth (); 
       
    System.out.println ("Y:" +y+ "M:" +m+ "D:" +d); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the AC 
    tion Bar if it is present. 
    Getmenuinflater (). Inflate (R.menu.main, menu); 
  return true; 
 } 
 
}
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.