5.2 Android Basic QuickStart Widgets&Other View Time Picker
2011-03-25 16:24
387 查看
Time Picker
演练时间选择控件。
在本教程中当用户单击按钮后我们将创建TimePickerDialog。从对话框选择的时间将被更新到TextView中。
新建项目HelloTimePicker。
打开res/layout/main.xml使用下面的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView android:id="@+id/timeDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<Button android:id="@+id/pickTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change the time"/>
</LinearLayout>
3. 打开HelloTimePicker.java 创建成员变量:
private TextView mTimeDisplay;
private Button mPickTime;
private int mHour;
private int mMinute;
static final int TIME_DIALOG_ID=0;
4. 修改OnCreate()方法的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//capture our view elements
mTimeDisplay = (TextView)findViewById(R.id.timeDisplay);
mPickTime = (Button)findViewById(R.id.pickTime);
//add a click listener to THE button
mPickTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
//get the current time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute= c.get(Calendar.MINUTE);
// dispaly the current date
updateDisplay();
}
5. 增加updateDisplay()方法和pad()方法:
//update the time we display in the Textview
private void updateDisplay(){
mTimeDisplay.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);
}
6. 增加一个TimePickerDialog.OnTimeSetListener的成员,当用户设置新的时间时被调用:
//the callback received when the user 'sets' the time in the dialog
private TimePickerDialog.OnTimeSetListener mTimeSetListener=
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
mHour = hourOfDay;
mMinute=minute;
updateDisplay();
}
};
7. 添加onCreateDialog(int)回调方法:
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener,mHour,mMinute,false);
}
return null;
}
8. 运行应用程序
author: im@xingquan.org
演练时间选择控件。
在本教程中当用户单击按钮后我们将创建TimePickerDialog。从对话框选择的时间将被更新到TextView中。
新建项目HelloTimePicker。
打开res/layout/main.xml使用下面的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView android:id="@+id/timeDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<Button android:id="@+id/pickTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change the time"/>
</LinearLayout>
3. 打开HelloTimePicker.java 创建成员变量:
private TextView mTimeDisplay;
private Button mPickTime;
private int mHour;
private int mMinute;
static final int TIME_DIALOG_ID=0;
4. 修改OnCreate()方法的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//capture our view elements
mTimeDisplay = (TextView)findViewById(R.id.timeDisplay);
mPickTime = (Button)findViewById(R.id.pickTime);
//add a click listener to THE button
mPickTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
//get the current time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute= c.get(Calendar.MINUTE);
// dispaly the current date
updateDisplay();
}
5. 增加updateDisplay()方法和pad()方法:
//update the time we display in the Textview
private void updateDisplay(){
mTimeDisplay.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);
}
6. 增加一个TimePickerDialog.OnTimeSetListener的成员,当用户设置新的时间时被调用:
//the callback received when the user 'sets' the time in the dialog
private TimePickerDialog.OnTimeSetListener mTimeSetListener=
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
mHour = hourOfDay;
mMinute=minute;
updateDisplay();
}
};
7. 添加onCreateDialog(int)回调方法:
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener,mHour,mMinute,false);
}
return null;
}
8. 运行应用程序
author: im@xingquan.org
相关文章推荐
- 5.2 Android Basic QuickStart Widgets&Other View Date Picker
- 5.2 Android Basic QuickStart Widgets&Other View Form Stuff
- 5.2 Android Basic QuickStart Widgets&Other View Spinner
- 5.2 Android Basic QuickStart Widgets&Other View Auto Complete
- 5.2 Android Basic QuickStart Widgets&Other View Web View
- 5.2 Android Basic QuickStart Widgets&Other View Gallery
- 5.2 Android Basic QuickStart Widgets&Other View Google Map View
- 5.1 Android Basic QuickStart Layouts List View
- 5.1 Android Basic QuickStart Layouts Grid View
- android中提示&对话框----ProgressDialog&DatePickerDialog &TimePickerDialog&PopupWindow
- android *** TimePickerDialog && DatePickerDialog
- Android自定义可循环的滚动选择器CycleWheelView 替代TimePicker/NumberPicker/WheelView
- 5.1 Android Basic QuickStart Layouts Relative Layout
- 5. Android Basic QuickStart Source Code
- 5.1 Android Basic QuickStart Layouts Table Layout
- Android开发教程 --- 葵花宝典第五层 控件之 Menu DatePicker TimePicker AutoCompleteTextView MultiAutoCompleteTextView
- 【Android开发】控件篇(一):TimePicker & NumberPicker
- Android Api Demos登顶之路(105)View-->Date widgets-->dialog
- DatePicker && TimePicker 【Android】
- 5.1 Android Basic QuickStart Layouts Tab Layout