第一种:
日期控件
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); } }
date.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>
第二种:
DateTime.java 时间和日期控件
import java.util.Calendar; import java.util.Locale; import android.app.Activity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TextView; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class DateTime extends Activity { private int my_year,my_month,my_day,my_hour,my_minute; private DatePicker date_picker; private TimePicker time_picker; private TextView text_view; private Calendar calendar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.date_view); //设置时间为中国 calendar = Calendar.getInstance(Locale.CHINA); //获取日期 my_year = calendar.get(Calendar.YEAR); my_month = calendar.get(Calendar.MONTH); my_day = calendar.get(Calendar.DAY_OF_MONTH); my_hour = calendar.get(Calendar.HOUR_OF_DAY); my_minute = calendar.get(Calendar.MINUTE); //获取控件 date_picker = (DatePicker)findViewById(R.id.datepicker); time_picker = (TimePicker)findViewById(R.id.timepicker); text_view = (TextView)findViewById(R.id.showdate_time); //设置时间格式为24小时 time_picker.setIs24HourView(true); //显示时间 text_view.setText(my_year+"/"+(my_month+1)+"/"+my_day+" "+my_hour+":"+my_minute); //日历控件 date_picker.init(my_year, my_month, my_day, new OnDateChangedListener() { //日期修改的单击事件 @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { my_year = year; my_month = monthOfYear; my_day = dayOfMonth; //显示时间 text_view.setText(my_year+"/"+(my_month+1)+"/"+my_day+" "+my_hour+":"+my_minute); } }); //为时间控件添加事件 time_picker.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { my_hour = hourOfDay; my_minute = minute; //显示时间 text_view.setText(my_year+"/"+(my_month+1)+"/"+my_day+" "+my_hour+":"+my_minute); } }); } }
date_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <DatePicker android:id="@+id/datepicker" android:layout_height="wrap_content" android:layout_width="wrap_content"></DatePicker> <TimePicker android:id="@+id/timepicker" android:layout_height="wrap_content" android:layout_width="wrap_content"></TimePicker> <TextView android:id="@+id/showdate_time" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView> </LinearLayout>
Powered by: 博客园 Copyright © 2024 txf2004 Powered by .NET 8.0 on Kubernetes