Android control Development Timepicker and DatePicker

Source: Internet
Author: User

Android control Development Timerpicker and DatePicker


Java code:Package com.example.timepicker;


Import Android.os.Bundle;
Import android.app.Activity;
Import Android.app.DatePickerDialog;
Import Android.app.Dialog;
Import Android.app.TimePickerDialog;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.DatePicker;
Import Android.widget.TimePicker;


public class Mainactivity extends Activity {


Private final int TIMER = 1;
Private final int DATE = 2;

Private Button Showtimerbutton = null;
Private Button Showdatebutton = null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Showtimerbutton = (Button) Findviewbyid (R.id.showtimepicker);

Showtimerbutton.setonclicklistener (New Showtimerdialoglistener ());

Showdatebutton = (Button) Findviewbyid (R.id.showdatepicker);

Showdatebutton.setonclicklistener (New Showdatedialoglistener ());

}

Listener for Date button
Class Showdatedialoglistener implements onclicklistener{


@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Show Date Diaolog
ShowDialog (DATE);
}

}



Timer Button Listener
Class Showtimerdialoglistener implements onclicklistener{


@Override
public void OnClick (View v) {
TODO auto-generated Method Stub

Show dialog with ID timer
ShowDialog (TIMER);

Timepickerdialog Timerdialog = new Timepickerdialog (this, Ontimesetlistener, Si, si, true);
Timerdialog.show ();
}

}




This method is called by ShowDialog () and passed in the dialog ID
protected Dialog oncreatedialog (int id) {
Dialog Dialog = null;
Switch (ID) {
When the ID is timer, a Timepickerdialog object is created and returned
Case TIMER:
dialog = new Timepickerdialog (this, Ontimesetlistener, Si, si, true);
Break
When the ID is date, a Datepickerdialog object is created and returned
Case DATE:
dialog = new Datepickerdialog (This, Ondatesetlistener, 2014, 12, 10);
Break
}
return dialog;
}




The main.xml file 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: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 ">


<textview
Android:id= "@+id/mytext"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>

<button
Android:id= "@+id/showtimepicker"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/mytext"
android:text= "@string/show_timer"/>

<button
Android:id= "@+id/showdatepicker"
android:layout_height= "Wrap_content"
Android:layout_width= "Fill_parent"
android:layout_below= "@id/showtimepicker"
android:text= "@string/show_date"/>


</RelativeLayout>


The Strings.xml code is as follows: <?xml version= "1.0" encoding= "Utf-8"?>
<resources>


<string name= "App_name" >PickerDialog</string>
<string name= "Action_settings" >Settings</string>
<string name= "Hello_world" >hello world!</string>
<string name= "Show_timer" >show time dialog</string>
<string name= "show_date" >show date dialog</string>
</resources>


The display results are as follows:Main interface:


Click the Show Timer Dialog button to appear as follows:


Click on Show Date dialog to appear as follows:


Android control Development Timepicker and DatePicker

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.