Picker(Time picker&& Date picker)
来源:互联网 发布:mac mini 散热器 编辑:程序博客网 时间:2024/04/29 06:52
Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs. Each picker provides controls for selecting each part of the time (hour, minute, AM/PM) or date (month, day, year). Using these pickers helps ensure that your users can pick a time or date that is valid, formatted correctly, and adjusted to the user's locale.
We recommend that you use DialogFragment
to host each time or date picker. The DialogFragment
manages the dialog lifecycle for you and allows you to display the pickers in different layout configurations, such as in a basic dialog on handsets or as an embedded part of the layout on large screens.
推荐使用DialogFragment
。
Although DialogFragment
was first added to the platform in Android 3.0 (API level 11), if your app supports versions of Android older than 3.0—even as low as Android 1.6—you can use the DialogFragment
class that's available in the support library for backward compatibility.
Note: The code samples below show how to create dialogs for a time picker and date picker using the support library APIs for DialogFragment
. If your app's minSdkVersion
is 11 or higher, you can instead use the platform version of DialogFragment
.
Creating a Time Picker
To display a TimePickerDialog
using DialogFragment
, you need to define a fragment class that extendsDialogFragment
and return a TimePickerDialog
from the fragment's onCreateDialog()
method.
Note: If your app supports versions of Android older than 3.0, be sure you've set up your Android project with the support library as described in Setting Up a Project to Use a Library.
Extending DialogFragment for a time picker
To define a DialogFragment
for a TimePickerDialog
, you must:
- Define the
onCreateDialog()
method to return an instance ofTimePickerDialog
- Implement the
TimePickerDialog.OnTimeSetListener
interface to receive a callback when the user sets the time.
Here's an example:
public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current time as the default values for the picker final Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); // Create a new instance of TimePickerDialog and return it return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity())); } public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // Do something with the time chosen by the user }}
See the TimePickerDialog
class for information about the constructor arguments.
Now all you need is an event that adds an instance of this fragment to your activity.
Showing the time picker
Once you've defined a DialogFragment
like the one shown above, you can display the time picker by creating an instance of the DialogFragment
and calling show()
.
For example, here's a button that, when clicked, calls a method to show the dialog:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pick_time" android:onClick="showTimePickerDialog" />
When the user clicks this button, the system calls the following method:
public void showTimePickerDialog(View v) { DialogFragment newFragment = new TimePickerFragment(); newFragment.show(getSupportFragmentManager(), "timePicker");}
This method calls show()
on a new instance of the DialogFragment
defined above. The show()
method requires an instance of FragmentManager
and a unique tag name for the fragment.
需要注意的是:如果低于3.0,需要使用getSupportFragmentManager()来代替getFragmentManager()并且要继承FragmentActivity而不是Activity。
效果:
Caution: If your app supports versions of Android lower than 3.0, be sure that you callgetSupportFragmentManager()
to acquire an instance of FragmentManager
. Also make sure that your activity that displays the time picker extends FragmentActivity
instead of the standard Activity
class.
Creating a Date Picker
Creating a DatePickerDialog
is just like creating a TimePickerDialog
. The only difference is the dialog you create for the fragment.
To display a DatePickerDialog
using DialogFragment
, you need to define a fragment class that extendsDialogFragment
and return a DatePickerDialog
from the fragment's onCreateDialog()
method.
Note: If your app supports versions of Android older than 3.0, be sure you've set up your Android project with the support library as described in Setting Up a Project to Use a Library.
Extending DialogFragment for a date picker
To define a DialogFragment
for a DatePickerDialog
, you must:
- Define the
onCreateDialog()
method to return an instance ofDatePickerDialog
- Implement the
DatePickerDialog.OnDateSetListener
interface to receive a callback when the user sets the date.
Here's an example:
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, year, month, day); } public void onDateSet(DatePicker view, int year, int month, int day) { // Do something with the date chosen by the user }}
See the DatePickerDialog
class for information about the constructor arguments.
Now all you need is an event that adds an instance of this fragment to your activity.
Showing the date picker
Once you've defined a DialogFragment
like the one shown above, you can display the date picker by creating an instance of the DialogFragment
and calling show()
.
For example, here's a button that, when clicked, calls a method to show the dialog:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pick_date" android:onClick="showDatePickerDialog" />
When the user clicks this button, the system calls the following method:
public void showDatePickerDialog(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getSupportFragmentManager(), "datePicker");}
This method calls show()
on a new instance of the DialogFragment
defined above. The show()
method requires an instance of FragmentManager
and a unique tag name for the fragment.
效果图:
Caution: If your app supports versions of Android lower than 3.0, be sure that you callgetSupportFragmentManager()
to acquire an instance of FragmentManager
. Also make sure that your activity that displays the time picker extends FragmentActivity
instead of the standard Activity
class.
- Picker(Time picker&& Date picker)
- Picker
- Date picker ...
- Date Picker
- MFC控件: Date Time Picker
- Date Time Picker控件的使用
- 基于Jquery UI的Date time picker
- MFC控件(10): Date Time Picker
- MFC控件之Date Time Picker
- MFC Date Time Picker的使用
- MFC控件(10): Date Time Picker
- JQuery Mobile: Date picker
- Date Picker解析
- jQuery Date Range Picker
- Date picker 控件简介
- MFC控件(10): Date Time Picker-使用及日期运算
- Date Range Picker 插件 bootstrap
- picker view
- MonoBehaviour的事件和具体功能总结
- C++中定义函数参数默认值
- eclipse, Log4j配置(真心的详细~)
- MIUI 6.2.2.0 稳定版 开启开发者模式
- 在Ubuntu上安装MiniGUI
- Picker(Time picker&& Date picker)
- RCFile文件格式
- 数据结构 队列
- mysql安装常见问题(系统找不到指定的文件、发生系统错误 1067 进程意外终止)
- Application.LoadLevel 加载关卡
- Android基础:教你如何获取软件自身版本号和Android系统版本号
- 一种trie树 的实现方式
- mysql root密码修改
- java web log4j配置