首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android控件之-DatePicker跟TimePicker

2012-09-01 
Android控件之-DatePicker和TimePicker显示日期和时间这两个类很经常用得到package com.ko8eimport java.

Android控件之-DatePicker和TimePicker

显示日期和时间

这两个类很经常用得到

package com.ko8e;import java.util.Calendar;import android.app.Activity;import android.app.DatePickerDialog;import android.app.TimePickerDialog;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.DatePicker;import android.widget.TextView;import android.widget.TimePicker;public class MyActivity extends Activity {/** Called when the activity is first created. */private TextView view = null;private DatePicker date = null;private TimePicker time = null;private Button button1 = null;private Button button2 = null;Calendar c;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);view = (TextView) findViewById(R.id.view);view.setText(R.string.view);// 取得系统时间对象c = Calendar.getInstance();date = (DatePicker) findViewById(R.id.date);time = (TimePicker) findViewById(R.id.time);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);button1.setText(R.string.button1);button2.setText(R.string.button2);date.init(c.get(Calendar.DAY_OF_YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH),new DatePicker.OnDateChangedListener() {public void onDateChanged(DatePicker view, int year,int monthOfYear, int dayOfMonth) {// 当日期改变时,在这里处理c.set(year, monthOfYear, dayOfMonth);}});// 设置为24小时制time.setIs24HourView(true);time.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {// 当前时间改变时,在这里处理c.set(c.get(Calendar.DAY_OF_YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND));}});button1.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {new DatePickerDialog(MyActivity.this,new DatePickerDialog.OnDateSetListener() {public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {// 设置日历}}, c.get(Calendar.DAY_OF_YEAR), c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();}});button2.setOnClickListener(new Button.OnClickListener() {public void onClick(View v) {new TimePickerDialog(MyActivity.this,new TimePickerDialog.OnTimeSetListener() {public void onTimeSet(TimePicker view,int hourOfDay, int minute) {// 设置时间}}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE),true).show();}});}}

main.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:id="@+id/view"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     /> <DatePicker android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content" /> </LinearLayout>

具体的代码实现好像有些wrong,有时间把它重新改一改。

热点排行