Androidの日付ピッカーの例
Androidでは、「http://developer.android.com/reference/android/widget/DatePicker.html[android.widget.DatePicker]」クラスを使用して、日付ピッカーコンポーネントをレンダリングし、定義済みのユーザーインターフェイス。
このチュートリアルでは、android.widget.DatePickerを介して現在のページで、またandroid.app.DatePickerDialogを介してダイアログボックスで日付ピッカーコンポーネントをレンダリングする方法を示します。 さらに、日付ピッカーコンポーネントで日付を設定する方法も示します。
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.
1. 日付ピッカー
「res/layout/main.xml」ファイルを開き、デモ用の日付ピッカー、ラベル、ボタンを追加します。
ファイル:res / layout / main.xml
P.S The “DatePickerDialog
” is declare in code, not XML.
2. コードコード
コードのコメントを読んでください。一目瞭然です。
ファイル:MyAndroidAppActivity.java
package com.example.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 The “DatePickerDialog” example above, is referenced from Google Android date picker example、若干の変更あり。
3. Demo
アプリケーションを実行してください。
1. 結果、「日付ピッカー」と「テキストビュー」は現在の日付に設定されます。
2. 「日付の変更」ボタンをクリックすると、DatePickerDialog
を介してダイアログボックスに日付ピッカーコンポーネントが表示されます。
3. 「日付の選択」と「textview」の両方が、選択した日付で更新されます。
ソースコードをダウンロード
ダウンロード–Android-DatePicker-Example.zip(16 KB)