編輯:關於 Android 程式設計
這篇的目標是釐清時間、日期與時鐘相關元件的設定方式。picker 這個字在原文裡就是「選擇器」的意思,四個元件各對應如下:
幾個重點整理:
OnTimeChangedListener。setCurrentHour 與 setCurrentMinute 改寫預設值。setIs24HourView(true) 可切換成 24 小時制。getCurrentHour 與 getCurrentMinute。模擬器 Android 4.2 的顯示效果(非 24 小時制):
真機 Android 2.3.7 的顯示效果(非 24 小時制):
真機 Android 2.3.7 的顯示效果(24 小時制):
對照這三張圖可以明顯看出,不同系統版本的原生外觀差異不小,新版在 4.x 之後走向了更簡潔的時鐘樣式,這也是為什麼正式專案通常會自訂樣式或改用 Material 元件。
Java 程式碼:
package com.haut.a07_timepicker;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private TimePicker timePicker;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = (TimePicker) findViewById(R.id.timePickerId);
button = (Button) findViewById(R.id.buttonId);
// 為 timePicker 建立監聽器
TimePickerListener timeListener = new TimePickerListener();
timePicker.setOnTimeChangedListener(timeListener);
// 為 button 建立監聽器
ButtonListener buttonListener = new ButtonListener();
button.setOnClickListener(buttonListener);
// 預設顯示當前時間,也可以手動指定
timePicker.setCurrentHour(12);
timePicker.setCurrentMinute(0);
// 設定為 24 小時制
timePicker.setIs24HourView(true);
}
class TimePickerListener implements OnTimeChangedListener {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
String time = hourOfDay + "點:" + minute + "分";
Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();
}
}
class ButtonListener implements OnClickListener {
public void onClick(View v) {
String time = timePicker.getCurrentHour() + "點:"
+ timePicker.getCurrentMinute() + "分";
Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
版面程式碼:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/folwer1"
tools:context=".MainActivity" >
<TimePicker
android:id="@+id/timePickerId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/buttonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="獲取設置時間"
android:layout_below="@id/timePickerId"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
</RelativeLayout>
值得注意的是,DatePicker 沒有像 TimePicker 那樣現成的 OnTimeChangedListener 介面,變更的即時回呼得靠對話框版本,這部分我在下一篇再補充。
模擬器 Android 4.2 效果圖:
手機 Android 2.3.7 效果圖:
Java 程式碼——這裡只能透過按鈕主動讀取目前選定值:
package com.haut.a07_datepicker;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
private DatePicker datePicker;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datePicker = (DatePicker) findViewById(R.id.datePickerId);
button = (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener {
public void onClick(View v) {
String date = datePicker.getYear() + "年:" + datePicker.getMonth()
+ "月:" + datePicker.getDayOfMonth() + "日";
Toast.makeText(MainActivity.this, date, Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
版面程式碼:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/leaf"
tools:context=".MainActivity" >
<DatePicker
android:id="@+id/datePickerId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/buttonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="獲取設置日期"
android:layout_below="@id/datePickerId"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
</RelativeLayout>
這裡有個一定要記住的坑:getMonth() 回傳的是 0 起始的月份,也就是一月會得到 0;而 getDayOfMonth() 是從 1 開始。直接顯示時記得 +1,否則使用者看到的日期會整整差一個月。
顯示的時間會跟著系統時間變化,程式碼非常簡單,版面裡放一個 <AnalogClock/> 標籤就結束了。
模擬器 Android 4.2 效果圖:
手機 Android 2.3.7 效果圖:
同樣會隨系統時間即時更新,這裡示範了用 textColor 與 textSize 調整外觀:
模擬器 Android 4.2 效果圖:
手機 Android 2.3.7 效果圖:
版面程式碼:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/folwer"
tools:context=".MainActivity" >
<DigitalClock
android:id="@+id/digitalClockId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:textColor="#ff0000"
android:textSize="30sp" />
</RelativeLayout>
補充一句現況:AnalogClock 與 DigitalClock 兩者都已被標記為 deprecated,官方建議改用 TextClock(可透過 format12Hour、format24Hour 與 timeZone 屬性自訂格式)。若只是單純顯示時間,TextClock 是更合適的選擇。具體操作等實際用到時再逐一補充。
用 ObjectAnimator 打造弧形菜單 ArcMenu
本文介紹如何以 ObjectAnimator 簡單實現一個弧形展開的菜單 ArcMenu。只要直接使用文中的 ArcMenu 類別,就能快速為應用加入這類扇形彈出菜單。使
Android 基本控制元件與四種常見布局詳解
Android 的介面由控制元件與布局共同構成。控制元件負責內容呈現與互動,布局則決定排列方式。本文整理常用控制元件,並說明四種常見布局,適合初學者建立完整概念。常用基本
Android TextView 文字描邊(字幕)效果的實作範例
想讓 TextView 的文字帶上描邊、呈現類似字幕的效果嗎?透過自訂一個繼承自 TextView 的元件,並在 onDraw 中搭配陰影與裁剪,就能輕鬆做到。本文分享一
Universal-Image-Loader 完全解析:從使用到源碼的圖片加載框架全攻略
做 Android 的過程中,幾乎每個人都要面對異步加載圖片這件事,尤其是列表裡一次性載入幾十上百張圖的時候。圖片加載從來不是「new 一個 Bitmap 塞進 Imag