本文主要是介绍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-sdkandroid:minSdkVersion="8"android:targetSdkVersion="15" /><applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name" ><activityandroid: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"><DatePickerandroid:id="@+id/datePicker"android:layout_width="match_parent"android:layout_height="wrap_content" /><TimePickerandroid:id="@+id/timePicker"android:layout_width="match_parent"android:layout_height="wrap_content" /><TextViewandroid: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;@Overridepublic 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);}@Overridepublic void onTimeChanged(TimePicker view, int hourOfDay, int minute) {Toast.makeText(this, "hourOfDay: " + hourOfDay + ", minute: " + minute,Toast.LENGTH_LONG).show();}@Overridepublic 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()));}
}
三、案例效果展示
这篇关于Android鸳鸯刀之DatePicker、TimePicker之明年今日的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!