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+""); }}