Android鸳鸯刀之DatePicker、TimePicker之明年今日
传送门 ☞ Android兵器谱 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229
鸳鸯刀
鸳鸯刀中藏有大秘密:仁者无敌!
今天我们学习如何利用Android平台“鸳鸯刀”DatePicker、TimePicker来设置日期和时间,下面给出该情景的案例:
一、案例技术要点
1.为DatePicker、TimePicker所在的Activity实现OnDateChangedListener、OnTimeChangedListener两类监听,并且为它们引入这两个监听。
2.datePicker.init(...):初始化日期。
3.timePicker.setIs24HourView(true):设置时间显示格式为24小时制。
二、案例代码陈列
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.datepicker" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".DateTimePickerMainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>strings.xml
<resources> <string name="app_name">DateTimePicker输入日期时间</string> </resources>main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <DatePicker android:id="@+id/datePicker" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/timePicker" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout>DateTimePickerMainActivity.java
package com.android.datepicker; import java.text.SimpleDateFormat; import java.util.Calendar; import android.app.Activity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.TextView; import android.widget.Toast; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; /** * DatePicker、TimePicker案例:设置日期时间 * 可输入日期(1900-1-1至2100-12-31) * @author lynnli1229 */ public class DateTimePickerMainActivity extends Activity implements OnDateChangedListener, OnTimeChangedListener { private TextView textView; private DatePicker datePicker; private TimePicker timePicker; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); datePicker = (DatePicker) findViewById(R.id.datePicker); timePicker = (TimePicker) findViewById(R.id.timePicker); textView = (TextView) findViewById(R.id.textView); // 初始化年月日 datePicker.init(2001, 1, 25, this); // 设置24小时制显示 timePicker.setIs24HourView(true); timePicker.setOnTimeChangedListener(this); } @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { Toast.makeText(this, "hourOfDay: " + hourOfDay + ", minute: " + minute, Toast.LENGTH_LONG).show(); } @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Calendar calendar = Calendar.getInstance(); calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), timePicker.getCurrentHour(), timePicker.getCurrentMinute()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm"); textView.setText(sdf.format(calendar.getTime())); } }三、案例效果展示