Android的DatePicker和TimePicker-android学习之旅(三十八)
DatePicker和TimePicker简单介绍
DatePicker和TimePicker是从FrameLayout继承而来。他们都是比較简单的组件。时间改变时间分别加入OnDateChangeListener()和OnTimeChangeListener()来监听。
DatePicker和TimePicker属性
选择日期和时间的实例
package peng.liu.test;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
private DatePicker datePicker;
TimePicker timePicker;
EditText edit;
private int year,month,day,hour,minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datePicker = (DatePicker) findViewById(R.id.datePicker);
timePicker = (TimePicker) findViewById(R.id.timePicker);
edit = (EditText) findViewById(R.id.show);
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
datePicker.init(year,month,day,new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker datePicker, int i, int i2, int i3) {
year = i;
month = i2;
day = i3;
showDate(year,month,day,hour,minute);
}
});
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker timePicker, int i, int i2) {
hour = i;
minute = i2;
showDate(year,month,day,hour,minute);
}
});
}
private void showDate(int year,int month,int day,int hour,int minute){
edit.setText(year+month+day+hour+day+"");
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选择详细的时间"/>
<DatePicker
android:layout_width="wrap_content"
android:layout_height="200dp"
android:id="@+id/datePicker"
android:startYear="2000"
android:endYear="2015"
android:layout_gravity="center_horizontal"
android:spinnersShown="true"
android:calendarViewShown="true"/>
<TimePicker
android:layout_width="wrap_content"
android:layout_height="100dp"
android:id="@+id/timePicker"
android:layout_gravity="center_horizontal"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/show"
android:editable="false"
android:cursorVisible="false"/>
</LinearLayout>