Android time and date controls can be used in either of the following ways:

Source: Internet
Author: User
Tags time and date

First:

Date Control

DatePicker. java

package com.example.dataPicker;import android.app.Activity;import android.app.DatePickerDialog;import android.app.Dialog;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.DatePicker;public class dataPicker extends Activity {    /** Called when the activity is first created. */    private Button ShowDataPickerButton = null;    private static final int DATA_PICKER_ID = 1;        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        ShowDataPickerButton = (Button)findViewById(R.id.ShowDataPickerButton);        ShowDataPickerButton.setOnClickListener(new ShowClickListener());    }    private class ShowClickListener implements  OnClickListener{        @Override        public void onClick(View arg0) {            // TODO Auto-generated method stub            showDialog(DATA_PICKER_ID);        }            }    DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener(){        @Override        public void onDateSet(DatePicker view, int year, int month, int day) {            // TODO Auto-generated method stub            System.out.println(year + "-" + month +"-" + day);        }            };    @Override    protected Dialog onCreateDialog(int id) {        // TODO Auto-generated method stub        switch(id){        case DATA_PICKER_ID:            return new DatePickerDialog(this,onDateSetListener,2011,3,22);        }        return super.onCreateDialog(id);    }}

 

TimePicker. java

package com.yongche.test;import android.app.Activity;import android.app.DatePickerDialog;import android.app.TimePickerDialog;import android.app.Dialog;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.DatePicker;import android.widget.TimePicker;public class timePicker extends Activity {    /** Called when the activity is first created. */    private Button ShowDataPickerButton = null;    private static final int DATA_PICKER_ID = 1;        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.data);        ShowDataPickerButton = (Button)findViewById(R.id.ShowDataPickerButton);        ShowDataPickerButton.setOnClickListener(new ShowClickListener());    }    private class ShowClickListener implements  OnClickListener{        @Override        public void onClick(View arg0) {            // TODO Auto-generated method stub            showDialog(DATA_PICKER_ID);        }            }    TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener(){        @Override        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {            // TODO Auto-generated method stub            System.out.println(hourOfDay + "-" + minute);        }            };    @Override    protected Dialog onCreateDialog(int id) {        // TODO Auto-generated method stub        switch(id){        case DATA_PICKER_ID:            return new TimePickerDialog(this,onTimeSetListener,22,3, true);        }        return super.onCreateDialog(id);    }}

 

Same data. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    />    <Button       android:id="@+id/ShowDataPickerButton"       android:layout_width="fill_parent"       android:layout_height="wrap_content"     />        </LinearLayout>
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.