Android 日期和時間的使用實例詳解
Android 日期和時間的使用
日期和時間的使用;
1:彈出框TimePickerDialog,DatePickerDialog
2:組件TimePicker,DatePicker
TimePickerDialog的使用:通過點擊button顯示圖一,然后用戶可以設(shè)置時間
DatePickerDialog的使用只需要將TimePickerDialog修改成DatePickerDialog, TimePickerDialog.OnTimeSetListener 分別修改成DatePickerDialog,OnDateSetListener既可
public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { //用戶創(chuàng)建彈出時間框的方法 @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 } }
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pick_time" android:onClick="showTimePickerDialog" />
public void showTimePickerDialog(View v) { DialogFragment newFragment = new TimePickerFragment(); newFragment.show(getSupportFragmentManager(), "timePicker"); }
DatePickerDialog的代碼:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { //用戶創(chuàng)建日期對話框的時間方法 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day); return dialog; } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android ADT和SDK Manager無法更新下載解決方案
這篇文章主要介紹了Android ADT和SDK Manager無法更新下載解決方案的相關(guān)資料,需要的朋友可以參考下2017-04-04Android實現(xiàn)動態(tài)改變app圖標的示例代碼
本篇文章主要介紹了Android實現(xiàn)動態(tài)改變app圖標的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-09-09Android listview定位到上次顯示的位置的實現(xiàn)方法
這篇文章主要介紹了Android listview定位到上次顯示的位置的實現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-08-08