本文实例分析了android开发之timepicker控件用法。分享给大家供大家参考,具体如下:
新建项目:
new android project->
project name:hellospinner
build target:android 2.2
application name:hellospinner
package name:com.b510
create activity:mainactivity
min sdk version:9
finish
运行效果:
如果:
return new timepickerdialog(this, mtimesetlistener, mhour, mminute, false);
代码部分:
mainactivity.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package com.b510; import java.util.calendar; import android.app.activity; import android.app.dialog; import android.app.timepickerdialog; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; import android.widget.timepicker; public class hellotimepicker extends activity { /* 显示时间信息 */ private textview tvtimepickerdisplay; /* 设置时间按钮 */ private button btntimepicker; /* 小时 */ private int mhour; /* 分钟 */ private int mminute; /* 标识 dialog的id */ static final int time_dialog_id = 0; /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); this .tvtimepickerdisplay = (textview) this .findviewbyid(r.id.tv_timepicker_display); this .btntimepicker = (button) findviewbyid(r.id.btn_timepicker); btntimepicker.setonclicklistener(listener); // get the current time final calendar c = calendar.getinstance(); mhour = c.get(calendar.hour_of_day); mminute = c.get(calendar.minute); // display the current date updatedisplay(); } private onclicklistener listener = new onclicklistener() { @override public void onclick(view v) { showdialog(time_dialog_id); } }; // updates the time we display in the textview private void updatedisplay() { tvtimepickerdisplay.settext( new stringbuilder().append(pad(mhour)).append( ":" ) .append(pad(mminute))); } private static string pad( int c) { if (c >= 10 ) return string.valueof(c); else return "0" + string.valueof(c); } // the callback received when the user "sets" the time in the dialog private timepickerdialog.ontimesetlistener mtimesetlistener = new timepickerdialog.ontimesetlistener() { public void ontimeset(timepicker view, int hourofday, int minute) { mhour = hourofday; mminute = minute; updatedisplay(); } }; @override protected dialog oncreatedialog( int id) { switch (id) { case time_dialog_id: return new timepickerdialog( this , mtimesetlistener, mhour, mminute, true ); } return null ; } } |
main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?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" > <textview android:id= "@+id/tv_timepicker_display" android:layout_width= "fill_parent" android:layout_height= "wrap_content" /> <button android:id= "@+id/btn_timepicker" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "change the time" /> </linearlayout> |
希望本文所述对大家android程序设计有所帮助。