TimePicker的样子(4.2版本): java
下面,咱们来添加一个TimePicker:android
<RelativeLayout 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" > <TimePicker android:id="@+id/timePicker_1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
这样,就添加上了:git
声明成员变量:code
private TimePicker timePicker;
接着获取这个控件:xml
timePicker = (TimePicker) findViewById(R.id.timePicker_1);
而后实现TimePicker的监听器:对象
当TimePicker的时间发生改变时,就会调用这个监听器; 参数timePicker为所操做的TimePicker对象图片
class TimePickerListener implements OnTimeChangedListener { public void onTimeChanged(TimePicker timePicker, int hours, int minute) { System.out.println("当前选择的时间是:" + hours + ":" + minute); } }
接着生成监听器对象,并挂接好:utf-8
TimePickerListener listener = new TimePickerListener(); timePicker.setOnTimeChangedListener(listener);
此时当TimePicker的时间发生改变时,就会打印出改变的时间:get
很简单:it
TimePicker.setIs24HourView(true);
在按钮监听器的监听方法中使用TimePicker的get方法来获取时间:
class ButtonClickListener implements OnClickListener { public void onClick(View v) { int hour = timePicker.getCurrentHour(); int minute = timePicker.getCurrentMinute(); System.out.println("获取到的时间为:" + hour + ":" + minute); } }
4.2版本的DatePicker:
<RelativeLayout 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" > <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_GetDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/datePicker" android:text="Get Date"/> </RelativeLayout>
private DatePicker datePicker; private Button btn_GetDate;
btn_GetDate = (Button) findViewById(R.id.btn_GetDate); datePicker = (DatePicker) findViewById(R.id.datePicker);
建立并实现按钮监听器类:
class ButtonClickListener implements OnClickListener { public void onClick(View v) { //获取日期时间 int year = datePicker.getYear(); int month = datePicker.getMonth() + 1; int day = datePicker.getDayOfMonth(); System.out.println("获取到的日期为:" + year + "/" + month + "/" +day); } }
ButtonClickListener btnListener = new ButtonClickListener(); btn_GetDate.setOnClickListener(btnListener);
如今点击按钮,就能获取用户选择的日期:
设置DatePicker的默认日期
datePicker.updateDate(2013, 4, 2);
注意 java中,月份从0 开始算起;
这是模拟时钟:
声明:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <AnalogClock android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <DigitalClock android:id="@+id/digitalClock1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DigitalClock" /> </RelativeLayout>