【Android】時間與日期Widget(DatePicker 與 TimePicker)
java.lang.Object | ||||
↳ | android.view.View | |||
↳ | android.view.ViewGroup | |||
↳ | android.widget.FrameLayout | |||
↳ | android.widget.DatePicker |
Class Overview
This class is a widget for selecting a date. The date can be selected by a year, month, and day spinners or aCalendarView
.
The set of spinners and the calendar view are automatically synchronized. The client can customize whether only the spinners, or only the calendar view, or both to be displayed. Also the minimal and maximal date from which dates to be selected can be customized.
See theDate Picker tutorial.
To provide a widget for selecting a date, use theDatePicker
widget,
which allows the user to select the month, day, and year, in a familiar interface.
In this tutorial, you'll create aDatePickerDialog
, which presents
the date picker in a floating dialog box at the press of a button. When the date is set by the user, aTextView
will
update with the new date.
java.lang.Object | ||||
↳ | android.view.View | |||
↳ | android.view.ViewGroup | |||
↳ | android.widget.FrameLayout | |||
↳ | android.widget.TimePicker |
Class Overview
A view for selecting the time of day, in either 24 hour or AM/PM mode. The hour, each minute digit, and AM/PM (if applicable) can be conrolled by vertical spinners. The hour can be entered by keyboard input. Entering in two digit hours can be accomplished by
hitting two digits within a timeout of about a second (e.g. '1' then '2' to select 12). The minutes can be entered by entering single digits. Under AM/PM mode, the user can hit 'a', 'A", 'p' or 'P' to pick. For a dialog using this view, seeTimePickerDialog
.
See theTime Picker tutorial.
public class Activity01 extends Activity
{
TextView m_TextView;
//宣告dataPicker
DatePicker m_DatePicker;
//宣告TimePicker
TimePicker m_TimePicker;
Button m_dpButton;
Button m_tpButton;
//java中的Calendar類
Calendar c;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
c=Calendar.getInstance();
m_TextView= (TextView) findViewById(R.id.TextView01);
m_dpButton = (Button)findViewById(R.id.button1);
m_tpButton = (Button)findViewById(R.id.button2);
//獲取DataPicker物件
m_DatePicker = (DatePicker) findViewById(R.id.DatePicker01);
//初始化當前時間並設定監聽
m_DatePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
//當日期更改在這裡設定
c.set(year, monthOfYear, dayOfMonth);
}
});
//
m_TimePicker = (TimePicker) findViewById(R.id.TimePicker01);
//
m_TimePicker.setIs24HourView(true);
//
m_TimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
{
//當時間更改在這裡設定
c.set(year, month, day, hourOfDay, minute, second);
}
});
m_dpButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
new DatePickerDialog(Activity01.this,
new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
//設定日曆
}
},c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
}
});
m_tpButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
new TimePickerDialog(Activity01.this,
new TimePickerDialog.OnTimeSetListener()
{
public void onTimeSet(TimePicker view, int hourOfDay,int minute)
{
//設定時間
}
},c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true).show();
}
});
}
}
對應的layout檔案
<?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/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
<Button
android:id="@+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
</LinearLayout>
相關文章
- Android UI控制元件系列:DatePicker,TimePicker(日期和時間選擇)AndroidUI控制元件
- Python 日期與時間Python
- 使用mysql日期與時間函式輕易搞定日期與時間邏輯MySql函式
- SQL server 日期與時間操作SQLServer
- Flutter 日期時間DatePicker控制元件及國際化Flutter控制元件
- Android Setting下修改時間與日期格式的問題???Android
- golang日期字串與時間戳轉換Golang字串時間戳
- Qt獲取當前日期與時間QT
- WPS表格日期與時間函式函式
- Java 日期和時間 API:實用技巧與示例 - 輕鬆處理日期和時間JavaAPI
- 日期和時間的儲存與處理
- C#快速入門教程(25)—— 日期與時間C#
- js時間戳與日期格式的相互轉換JS時間戳
- MYSQL中UNIX時間戳與日期的轉換MySql時間戳
- C++中UNIX時間戳與日期互轉C++時間戳
- jQuery UI 支援的時間元件timepickerjQueryUI元件
- Java 8 的日期與時間問題解決方案Java
- 【萬里征程——Windows App開發】DatePicker&TimepickerWindowsAPP
- Java基礎篇(04):日期與時間API用法詳解JavaAPI
- PHP中比較兩個時間的大小與日期的差值PHP
- Android 時間戳和日期之間的轉化Android時間戳
- sencha:日期選擇元件datepicker元件
- android系統日期時間的獲取Android
- PHP的時間日期與例項應用:日曆核心程式PHP
- 陪玩原始碼,與時間、日期相關的程式碼分析原始碼
- Calendar類在Java中的應用與日期時間處理Java
- iOS日期時間iOS
- 日期時間類
- 日期和時間
- js將時間日期物件轉換為時間日期字元JS物件字元
- 鴻蒙Next應用國際化:時間與日期格式化鴻蒙
- js將時間日期字串轉換為時間日期Date物件JS字串物件
- Widget、RenderObject 與 ElementObject
- js獲取指定時間日期和當前時間日期的相差多少時間JS
- Java日期和時間Java
- JavaScript 時間日期操作JavaScript
- 日期時間函式函式
- Oracle時間日期操作Oracle