1· TimePicker的使用方法
TimePicker的样子(4.2版本):
下面,我们来添加一个TimePicker:
<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>
这样,就添加上了:
1.1 获取用户选择的时间:
声明成员变量:
private TimePicker timePicker;
接着获取这个控件:
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);
}
}
接着生成监听器对象,并挂接好:
TimePickerListener listener = new TimePickerListener();
timePicker.setOnTimeChangedListener(listener);
此时当TimePicker的时间发生改变时,就会打印出改变的时间:
1.2 实现使用24小时制的TimePicker:
很简单:
TimePicker.setIs24HourView(true);
1.3 实现点击按钮获取TimePicker的时间:
在按钮监听器的监听方法中使用TimePicker的get方法来获取时间:
class ButtonClickListener implements OnClickListener {
public void onClick(View v) {
int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
System.out.println("获取到的时间为:" + hour + ":" + minute);
}
}
2· DatePicker的使用方法
4.2版本的DatePicker:
2.1 添加DatePicker
- 声明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);
2.2 如何获取用户选择的日期
- 点击按钮获取日期
创建并实现按钮监听器类:
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 开始算起;
3· AnalogClock模拟时钟
这是模拟时钟:
3.1 添加模拟时钟
声明:
<?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>
4· 练习:
- 添加数字时钟:
<?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>
来源:oschina
链接:https://my.oschina.net/u/2437172/blog/495570