Android time Selector, date selector implementation code _android

Source: Internet
Author: User

This article for everyone to share the two selector, one can be for the time to choose, a can be selected for the date, for your reference, the specific content as follows

One, time selector
1.1. Layout

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout 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:orientation= "vertical" tools:context= "com.rj141.sb.kongjian.DateActivity" > <linearl Ayout android:orientation= "Horizontal" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:textSize= "20 DP "android:text=" Time to Eat: "/> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_ Content "Android:textsize=" 20DP "android:id=" @+id/tv "/> </LinearLayout> <button Android:layout_wi Dth= "Wrap_content" android:layout_height= "wrap_content" android:text= "Time" android:id= "@+id/btndate"/> </Line 
 Arlayout>

1.2.Java file

public class Dateactivity extends Actionbaractivity { 
 
 private Button btn; 
 Private TextView TV; 
 @Override 
 protected void onCreate (Bundle savedinstancestate) { 
 super.oncreate (savedinstancestate); 
 Setcontentview (r.layout.activity_date); 
 
 Btn= (Button) This.findviewbyid (r.id.btndate); 
 tv= (TextView) This.findviewbyid (r.id.tv); 
 Btn.setonclicklistener (New View.onclicklistener () { 
  @Override public 
  void OnClick (View v) { 
  new Timepickerdialog (Dateactivity.this, New Timepickerdialog.ontimesetlistener () { 
   @Override public 
   Void Ontimeset (timepicker view, int hourofday, int minute) { 
   Tv.settext (String.Format ("%d:%d", Hourofday,minute)); 
  //0,0 refers to the time, true indicates whether it is 24 hours, true is 24-hour 
  },0,0,true). Show () 
  ; 
 } 
 } 

Effect Chart:

Second, date selector
2.1.activity.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout 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:orientation= "vertical" 
tools:context= " Com.rj141.sb.kongjian.DateActivity "> 

<textview 
android:layout_width=" Match_parent " 
android:layout_height= "Wrap_content" 
android:textsize= "20DP" 
android:id= "@+id/tv"/> 

< Button 
android:layout_width= "wrap_content" 
android:layout_height= "wrap_content" 
android:text= " Calendar " 
android:id=" @+id/btndate "/> 
</LinearLayout> 

2.2.dateactivity.class

public class Dateactivity extends Actionbaractivity { 

private Button btn; 
Private TextView TV; 
@Override 
protected void onCreate (Bundle savedinstancestate) { 
super.oncreate (savedinstancestate); 
Setcontentview (r.layout.activity_date); 

Btn= (Button) This.findviewbyid (r.id.btndate); 
tv= (TextView) This.findviewbyid (r.id.tv); 
Btn.setonclicklistener (New View.onclicklistener () { 
@Override public 
void OnClick (View v) { 
new Datepickerdialog (Dateactivity.this, New Datepickerdialog.ondatesetlistener () { 
@Override public 
Void Ondateset (datepicker view, int year, int monthofyear, int dayofmonth) { 
Tv.settext ("Your date of birth is:" +string.format ("%d-% d-%d ", Year,monthofyear+1,dayofmonth)); 
} 
},2000,1,2). Show (); 
}} Datepickerdialog the Calendar Selector dialog box, listening for Ondatesetlistener () {...}

Effect Chart:

The above is the two Android time selector, the Android date selector implementation code, hoping to learn more about Android software programming help.

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.