編輯:關於 Android 程式設計
要讓使用者挑選日期或時間,Android 現成提供了兩個對話框元件:DatePickerDialog 負責年月日,TimePickerDialog 負責時分。兩者搭配 Calendar 使用,就能輕鬆完成時間設定功能。
畫面上放兩個 Button 分別觸發日期與時間選擇,再用一個 TextView 即時顯示目前設定的時間。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="https://schemas.android.com/apk/res/android" Android:orientation="vertical" Android:layout_width="fill_parent" Android:layout_height="fill_parent" > <TextView Android:id="@+id/dateAndTime" Android:layout_width="fill_parent" Android:layout_height="wrap_content" Android:text="@string/hello" /> <Button Android:id="@+id/setDate" Android:layout_width="fill_parent" Android:layout_height="wrap_content" Android:text="Set the Date"></Button> <Button Android:id="@+id/setTime" Android:layout_width="fill_parent" Android:layout_height="wrap_content" Android:text="Set the Time"></Button> </LinearLayout>
package yyl.Android;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;
import Android.app.Activity;
import Android.app.DatePickerDialog;
import Android.app.TimePickerDialog;
import Android.os.Bundle;
import Android.view.View;
import Android.widget.Button;
import Android.widget.DatePicker;
import Android.widget.TextView;
import Android.widget.TimePicker;
public class ChronoDemo extends Activity {
// 獲取日期格式器對象
DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
// 定義一個 TextView 控件對象
TextView dateAndTimeLabel = null;
// 獲取一個日歷對象
Calendar dateAndTime = Calendar.getInstance(Locale.CHINA);
// 當點擊 DatePickerDialog 控件的設置按鈕時,調用該方法
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// 修改日歷控件的年,月,日
dateAndTime.set(Calendar.YEAR, year);
dateAndTime.set(Calendar.MONTH, monthOfYear);
dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
// 將頁面 TextView 的顯示更新為最新時間
updateLabel();
}
};
TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
dateAndTime.set(Calendar.MINUTE, minute);
updateLabel();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 得到頁面設定日期的按鈕控件對象
Button dateBtn = (Button)findViewById(R.id.setDate);
dateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 生成一個 DatePickerDialog 對象,並顯示
new DatePickerDialog(ChronoDemo.this, d,
dateAndTime.get(Calendar.YEAR),
dateAndTime.get(Calendar.MONTH),
dateAndTime.get(Calendar.DAY_OF_MONTH)).show();
}
});
Button timeBtn = (Button)findViewById(R.id.setTime);
timeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new TimePickerDialog(ChronoDemo.this, t,
dateAndTime.get(Calendar.HOUR_OF_DAY),
dateAndTime.get(Calendar.MINUTE), true).show();
}
});
dateAndTimeLabel = (TextView)findViewById(R.id.dateAndTime);
updateLabel();
}
// 更新頁面 TextView 的方法
private void updateLabel() {
dateAndTimeLabel.setText(fmtDateAndTime.format(dateAndTime.getTime()));
}
}
第一,Calendar.MONTH 是從 0 開始計算的,一月要寫 0,顯示時記得加 1。第二,TimePickerDialog 最後一個參數代表是否使用 24 小時制,依產品需求決定即可。
Android 自訂 ViewGroup 之子控件自動換行與增刪
概述 常用的佈局類型並不能滿足所有需求,這時就需要用到 ViewGroup。 ViewGroup 作為容納 View 的容器,我們在編寫佈局 XML 時,會透過以 lay
Android 開發常用功能彙整:定時器、加密、圓角按鈕、線程與簽名
本文把日常 Android 開發中反覆用到的幾組實用功能整理在一起,涵蓋定時與延時、資料加密、圓角按鈕、Android Studio 調試與 Logcat、雙擊退出、線程
用 RxJava 打造酷炫啟動頁:timer 操作符與屬性動畫的組合
前言RxJava 在 GitHub 主頁上的自我介紹是:a library for composing asynchronous and event-based prog
Android 開發環境搭建圖文全解:JDK、Eclipse ADT 與模擬器
Android 開發可用的工具主要有 Eclipse 與 Android Studio 兩種,各有優劣。Studio 是 Google 自家推出的 IDE,理論上與平台整