package com.itarchy.datetime;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;
/**
*
* @E-mail: itarchy@163.com
* @version 创建时间:2015-11-2上午9:22:59
* @Des:
*/
public class MainActivity extends Activity {
// 声明日期控件对象
private DatePicker mDatePicker;
// 声明时间控件对象
private TimePicker mTimePicker;
private EditText etDate;
private EditText etTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initLayout();
setChangedLintener();
}
private void initLayout() {
mDatePicker = (DatePicker) this.findViewById(R.id.datePicker);
mTimePicker = (TimePicker) this.findViewById(R.id.timePicker);
etDate = (EditText) this.findViewById(R.id.et_date);
etTime = (EditText) this.findViewById(R.id.et_time);
}
private void setChangedLintener() {
// 1、获取年、月、日
Calendar mCalendar = Calendar.getInstance();
int year = mCalendar.get(Calendar.YEAR);
int monthOfYear = mCalendar.get(Calendar.MONTH);
int dayOfMonth = mCalendar.get(Calendar.DAY_OF_MONTH);
// 2、初始化DatePicker对象
mDatePicker.init(year, monthOfYear, dayOfMonth,
new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// 3、 日期改变的时候执行
String value = "您选择的日期是:" + year + "年"
+ (monthOfYear + 1) + "月" + dayOfMonth + "日";
Toast.makeText(getApplication(), value, 1000).show();
etDate.setText(value);
}
});
// 时间控件的初始化
mTimePicker.setIs24HourView(true);
mTimePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// 时间改变时执行
String value = "您设置的时间是:" + hourOfDay + ":" + minute;
Toast.makeText(getApplication(), value, 1000).show();
etTime.setText(value);
}
});
}
}
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="DatePicker|TimePicker"
android:textSize="25sp" />
<TextView
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:text="请选择日期:"
android:textColor="#FF0000"
android:textSize="25sp" />
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_date" />
<EditText
android:id="@+id/et_date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/datePicker"
android:hint="还没有修改日期哦!"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_date"
android:text="请设置时间:"
android:textColor="#FF0000"
android:textSize="25sp" />
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_time" />
<EditText
android:id="@+id/et_time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/timePicker"
android:hint="25:00"
android:textSize="15sp" />
</RelativeLayout>
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itarchy.datetime"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itarchy.datetime.MainActivity"
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>