這一個範例我們要來練習 DatePickerDialog與TimePickerDialog,
這兩個分別就是設定
DatePickerDialog:日期(年、月、日)
TimePickerDialog:時間(幾點記分)
這一個範例,我們用了兩個TextView、兩個Button來練習,
當按鈕按下去,就會分別顯示這兩個Dialog視窗,提供User做設定,
設定完成後會將設定結果顯示在TextView上,這就是這一次簡單的練習。
成果如下圖:
layout XML如下:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.test.test1.DateActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_datetime"/>
<TextView
android:id="@+id/datetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請設定日期" />
<Button
android:id="@+id/buttonDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:onClick="setDate"/>
<TextView
android:id="@+id/timetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請設定時間"
/>
<Button
android:id="@+id/buttonTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tiem"
android:onClick="setTime"/>
</LinearLayout>
</RelativeLayout>
Activity 程式如下:
package com.test.test1; import java.util.Calendar; import java.util.GregorianCalendar; import android.support.v7.app.ActionBarActivity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import android.widget.TimePicker; public class DateActivity extends ActionBarActivity { private Button doSetDate; private Button doSetTime; private TextView textDate; private TextView textTime; private DatePickerDialog datePickerDialog; private TimePickerDialog timePickerDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_date); doFindView(); GregorianCalendar calendar = new GregorianCalendar(); // 實作DatePickerDialog的onDateSet方法,設定日期後將所設定的日期show在textDate上 datePickerDialog = new DatePickerDialog(this, new OnDateSetListener() { //將設定的日期顯示出來 @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { textDate.setText(year + "/" + monthOfYear + "/" + dayOfMonth); } }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); // 實作TimePickerDialog的onTimeSet方法,設定時間後將所設定的時間show在textTime上 timePickerDialog = new TimePickerDialog(this, new OnTimeSetListener() { //將時間轉為12小時製顯示出來 @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { textTime.setText((hourOfDay > 12 ? hourOfDay - 12 : hourOfDay) + ":" + minute + " " + (hourOfDay > 12 ? "PM" : "AM")); } }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(calendar.MINUTE), false); } // 連接layout上的物件 public void doFindView() { doSetDate = (Button) findViewById(R.id.buttonDate); doSetTime = (Button) findViewById(R.id.buttonTime); textDate = (TextView) findViewById(R.id.datetext); textTime = (TextView) findViewById(R.id.timetext); } // setDate Button onClick 顯示日期設定視窗 public void setDate(View v) { datePickerDialog.show(); } // setTime Button onClick 顯示時間設定視窗 public void setTime(View v) { timePickerDialog.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.date, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
好了,這一次的練習是不是很簡單呢?
如果大家想做一個自己的鬧鐘APP,就可以運用到這一個工具來達到設定時間的功能,
那就讓大家發揮想像力去練習吧,希望這次得教學可以給各位一些幫助。
留言列表