我将Android控件的DatePicker和TimerPicker的学习知识总结一下和大家共享
在Android开发中,日期时间显示也是比较常见常用的控件。Android sdk提供了DatePicker和TimerPicker,分别以可视化的输入日期和时间。
DatePicker控件的基本使用方法很简单,在布局文件中使用<Button>既可以了,或者在java代码:DatePicker mDatePicker= (DatePicker)findViewById(R.id.datePicker1),TimerPicker也是一样的。
1、DatePicker控件
DatePicker控件可以输入日期。日期的输入范围是1991-1-1~2100-12-31。DatePicker控件是通过DatePicker类的getYear、getMonth、getDatOfMonth方法分别获得DatePicker控件显示当前的年、月、日。通过DatePicker类的init方法对DatePicker控件进行初始化。
public oid init(int year,int monthOfyear, int dayOfMonth, OnDateChangedListener onDateChangedListener )
其中onDateChangedListener 参数是用来设置DatePicker控件的日期变化监听对象。
2、TimerPicker控件
TimerPicker控件用来输入时间(只能输入小时和分钟)。TimerPicker在默认的情况下是12小时进制,如果想以24小时进制显示时间,可以使用TimerPicker.setIs24HourView方法设置。当TimerPicker的时间变化时,会触发OnTimeChanged事件,但与DatePicker控件不同的是,TimerPicker通过色tOnTimeChange的Listener方法设置监听时间变化的监听对象,而DatePicker通过Init方法设置监听日期变化的监听对象。
下面还是看例子:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<DatePicker android:id="@+id/datepicker"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TimePicker android:id="@+id/timepicker"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:id="@+id/textview" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="18dp" />
</LinearLayout>
java文件:
public class Main extends Activity implements OnDateChangedListener,
OnTimeChangedListener
{
private TextView textView;
private DatePicker datePicker;
private TimePicker timePicker;
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
{
onDateChanged(null, 0, 0, 0);
}
@Override
public 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()));
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
datePicker = (DatePicker) findViewById(R.id.datepicker);
timePicker = (TimePicker) findViewById(R.id.timepicker);
datePicker.init(2001, 1, 25, this);
timePicker.setIs24HourView(true);
timePicker.setOnTimeChangedListener(this);
textView = (TextView) findViewById(R.id.textview);
onDateChanged(null, 0, 0, 0);
}
}
3、Android还有比较直观的显示时间控件----时钟控件AnalogClock和DigitalClock
<strong style="font-family: Arial; background-color: rgb(255, 255, 255);"></strong>
下面看个代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center_horizontal">
<AnalogClock android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<DigitalClock android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="18dp" />
</LinearLayout>