传送门 ☞ Android兵器谱 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229
碧水剑
岳灵珊转头向山谷瞧了一眼,叫道:“这把剑,这把剑!”令狐冲又是一惊,知道小师妹的长剑是一口断金削铁的利器,叫做“碧水剑”,三年前师父在浙江龙泉得来,小师妹一见之下爱不释手,向师父连求数次,师父始终不给,直至今年她十八岁生日,师父才给了她当生日礼物,这一下堕入了深谷,再也难以取回,今次当真是铸成大错了。
今天我们学习如何利用Android平台“碧水剑”DatePickerDialog、TimePickerDialog来显示日期时间对话框,下面给出该情景的案例:
一、案例技术要点
1.时间选择对话框构造方法:new TimePickerDialog(...)。
第二个参数表示引入时间设置监听器对象MyTimeSetListener
最后一个参数标示是否按24小时制显示
2.日期选择对话框构造方法:new DatePickerDialog(...)。
第二个参数表示引入日期设置监听器对象MyDateSetListener
3.为TimePickerDialog新增自定义的时间设置监听MyTimeSetListener,并且实现OnTimeSetListener接口的onTimeSet(...)方法。
4.为DatePickerDialog新增自定义的日期设置监听MyDateSetListener,并且实现OnDateSetListener接口的onDateSet(...)方法。
二、案例代码陈列
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.clock" 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=".DateTimePickerDialogMainActivity" 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">DateTimePickerDialog显示日期时间对话框</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" > <AnalogClock android:layout_width="match_parent" android:layout_height="wrap_content" /> <DigitalClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp"/> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示TimePickDialog" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示DatePickDialog" /> </LinearLayout>DateTimePickerDialogMainActivity.java
package com.android.clock; import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; 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; import android.widget.Toast; /** * DatePickerDialog、TimePickerDialog案例:日期时间显示对话框 * @author lynnli1229 */ public class DateTimePickerDialogMainActivity extends Activity implements OnClickListener { private Button button1, button2; private int hourOfDay, minute, year, monthOfYear, dayOfMonth; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); //获取当前时间 Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); monthOfYear = calendar.get(Calendar.MONTH); dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); minute = calendar.get(Calendar.MINUTE); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: TimePickerDialog timePickerDialog = new TimePickerDialog(this, new MyTimeSetListener(), hourOfDay, minute, true); timePickerDialog.show(); break; case R.id.button2: DatePickerDialog datePickerDialog = new DatePickerDialog(this, new MyDateSetListener(), year, monthOfYear, dayOfMonth); datePickerDialog.show(); break; } } private class MyTimeSetListener implements OnTimeSetListener { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(DateTimePickerDialogMainActivity.this, "hourOfDay: " + hourOfDay + ", minute: " + minute, Toast.LENGTH_LONG).show(); } } private class MyDateSetListener implements OnDateSetListener { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(DateTimePickerDialogMainActivity.this, "year: " + year + ", monthOfYear: " + monthOfYear + ", dayOfMonth: " + dayOfMonth, Toast.LENGTH_LONG).show(); } } }三、案例效果展示