Androidでは、 “http://developer.android.com/reference/android/widget/DatePicker.html[android.widget.DatePicker]”クラスを使用して、日付ピッカーコンポーネントをレンダリングして、日、月、年を選択できます。あらかじめ定義されたユーザーインターフェイス。
このチュートリアルでは、http://developer.android.com/reference/android/widget/DatePicker.html[android.widget.DatePicker]を介して現在のページの日付ピッカーコンポーネントをレンダリングする方法と、
android.app.DatePickerDialog
また、日付ピッカーコンポーネントで日付を設定する方法も示します。
P.SこのプロジェクトはEclipse 3.7で開発され、Android 2.3.3.
でテストされています
1. DatePicker
”
res/layout/main.xml
“ファイルを開き、デモ用の日付ピッカー、ラベル、ボタンを追加します。
File:res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout__width="fill__parent"
android:layout__height="fill__parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnChangeDate"
android:layout__width="wrap__content"
android:layout__height="wrap__content"
android:text="Change Date"/>
<TextView
android:id="@+id/lblDate"
android:layout__width="wrap__content"
android:layout__height="wrap__content"
android:text="Current Date (M-D-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/tvDate"
android:layout__width="wrap__content"
android:layout__height="wrap__content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"/>
<DatePicker
android:id="@+id/dpResult"
android:layout__width="wrap__content"
android:layout__height="wrap__content"/>
</LinearLayout>
__P.S “DatePickerDialog`”はXML./ではなくコードで宣言されています
2.コードコード
コードのコメントを読んで、それは自明でなければなりません。
File:MyAndroidAppActivity.java
package com.mkyong.android;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class MyAndroidAppActivity extends Activity {
private TextView tvDisplayDate;
private DatePicker dpResult;
private Button btnChangeDate;
private int year;
private int month;
private int day;
static final int DATE__DIALOG__ID = 999;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setCurrentDateOnView();
addListenerOnButton();
}
//display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
dpResult = (DatePicker) findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY__OF__MONTH);
//set current date into textview
tvDisplayDate.setText(new StringBuilder()
//Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
//set current date into datepicker
dpResult.init(year, month, day, null);
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE__DIALOG__ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE__DIALOG__ID:
//set date picker as current date
return new DatePickerDialog(this, datePickerListener,
year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener() {
//when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
//set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
//set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
}
P.S上記の「DatePickerDialog」の例は、http://developer.android.com/resources/tutorials/views/hello-datepicker.html[Google Androidの日付ピッカーの例]
から参照されていますが、若干の変更が加えられています。
デモ
アプリケーションを実行します。
{空} 1。結果、「日付選択」および「テキストビュー」は現在の日付に設定されます。
{空} 2。 “Change Date”ボタンをクリックすると、DatePickerDialogを介してダイアログボックスに日付ピッカーコンポーネントが表示されます。
{空} 3。 「日付ピッカー」と「テキストビュー」の両方が選択された日付で更新されます。
ソースコードをダウンロードする
ダウンロードする –
Android-DatePicker-Example.zip
(16 KB)
参考文献
DatePickerの例]。
http://developer.android.com/reference/android/widget/DatePicker.html
[Android
DatePicker JavaDoc]。
http://developer.android.com/reference/android/app/DatePickerDialog.html
[Android
DatePickerDialog JavaDoc]
リンク://タグ/アンドロイド/[アンドロイド]リンク://タグ/日付/[日付]
日付ピッカー