TimePicker 和TimePickerDiag
先建立以个xml,然后拖拽TimerPicker;
TimePicker和TimePickerDiag
TimerPicker :
先建立xml文件,然后拖拽TimePicker然后在MainActivity中实例化 同时还需要添加Calender ,hour,minute
并通过单例模式获取calender的对象,然后获取hour,minute, 接着设置一个setTitle覆盖初始的title
通过timePicker.setOnTimeChangedListener()匿名内部类方法设置监听实现
通过new TimePickerDiag 实现
package com.example.timepickerdiag; import java.util.Calendar; import android.app.Activity; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class MainActivity extends Activity { private Calendar calendar; private TimePicker timePicker; private int hour; private int minute; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); calendar = calendar.getInstance(); hour = calendar.get(calendar.HOUR); minute = calendar.get(calendar.MINUTE); timePicker = (TimePicker) findViewById(R.id.timePicker1); setTitle(hour + "-" + minute); timePicker.setOnTimeChangedListener(new OnTimeChangedListener() { public void onTimeChanged(TimePicker view, int hour, int minute) { // TODO Auto-generated method stub setTitle(hour + "-" + minute); } }); new TimePickerDialog(this, new OnTimeSetListener() { public void onTimeSet(TimePicker view, int hour, int minute) { // TODO Auto-generated method stub setTitle(hour + "-" + minute); } }, calendar.HOUR, minute, true).show(); } }
DatePicker和DatePickerDiag
DatePicker :
先建立xml文件,然后拖拽DatePicker然后在MainActivity中实例化 同时还需要添加Calender ,year,month,day
并通过单例模式获取calender的对象,然后获取year ,month,day, 接着设置一个setTitle覆盖初始的title
通过datePicker.init()方法设置监听实现后
通过new datePickerDiag 设置监听
package com.example.datepickerdiag; import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.os.Bundle; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; public class MainActivity extends Activity { private Calendar calendar; private DatePicker datePicker; private int year; private int month; private int day; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); calendar = calendar.getInstance();// 获取calender的对象 year = calendar.get(calendar.YEAR); month = calendar.get(calendar.MONTH) + 1; day = calendar.get(calendar.DAY_OF_MONTH); datePicker = (DatePicker) findViewById(R.id.datePicker1); setTitle(year + "-" + (calendar.MONTH + 1) + "-" + day); datePicker.init(year, calendar.MONTH, calendar.DAY_OF_MONTH, new OnDateChangedListener() { public void onDateChanged(DatePicker view, int year, int month, int day) { // TODO Auto-generated method stub setTitle(year + "-" + (month + 1) + "-" + day); // 要的是参数 } }); new DatePickerDialog(this, new OnDateSetListener() { public void onDateSet(DatePicker view, int year, int month, int day) { // TODO Auto-generated method stub setTitle(year + "-" + (month + 1) + "-" + day); } }, year, month, day).show(); } }