開発したアプリなど一覧

Android Tutorials DatePicker and TimePicker

記事内にアフィリエイトリンクを含む場合があります

DatePickerTimePicker は似たようなサンプルだから一つに纏めようとしたらすんなり動いた。
それなりに理解してきたかな?

画面はXML、他は全部Javaで書く。画面もJavaで書けるけど非推奨の様です。画面いじる様にレイアウトエディターがあるけどこれはどうなんだろう・・・ なんだか良くわからないですね。

エミュレータが起動も動作も超もっさりなのは我慢ですかね・・・

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/dateDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> <Button android:id="@+id/pickDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change the date"/> <Button android:id="@+id/pickTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change the time"/> </LinearLayout>

HelloDatePicker.java

package com.example.hellodatepicker;

import java.util.Calendar;

import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; 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 HelloDatePicker extends Activity { /** Called when the activity is first created. */ private TextView mDateDisplay; private Button mPickDate; private Button mPickTime;

private int mYear; private int mMonth; private int mDay; private int mHour; private int mMinute;

static final int DATE_DIALOG_ID = 0; static final int TIME_DIALOG_ID = 1;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

// capture our view elements mDateDisplay = (TextView) findViewById(R.id.dateDisplay); mPickDate = (Button) findViewById(R.id.pickDate); mPickTime = (Button) findViewById(R.id.pickTime);

// add a click listener to the button mPickDate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(DATE_DIALOG_ID); } }); mPickTime.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(TIME_DIALOG_ID); } });

// get the current date final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); mHour = c.get(Calendar.HOUR_OF_DAY); mMinute = c.get(Calendar.MINUTE);

// display the current date updateDisplay(); }

@Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); case TIME_DIALOG_ID: return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false); } return null; }

private void updateDisplay() { mDateDisplay.setText( new StringBuilder() // Month is 0 based so add 1 .append(mMonth + 1).append("-") .append(mDay).append("-") .append(mYear).append(" ") .append(pad(mHour)).append(":") .append(pad(mMinute))); }

// the callback received when the user "sets" the date in the dialog private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; updateDisplay(); } };

private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mHour = hourOfDay; mMinute = minute; updateDisplay(); } };

private static String pad(int c) { if (c >= 10) return String.valueOf(c); else return "0" + String.valueOf(c); } }

Sponsored Link

コメント

  1. Luc より:

    Thanks a lot for this tutorial.
    In fact, I’m now trying to use DatePicker and TimePicker but within a Dialog instead of an activity. I have pb to do so… Would you have any idea why this can go wrong ? Especially, how would you pass a context to the DatePicker and TimePicker ?
    Thanks a lot,
    Luc

  2. NCudi より:

    thank you soooooooooooo much :~))))))

  3. Moh より:

    mPickTime = (Button) findViewById(R.id.pickTime);

    pickTime cannot be resolved or is not a field

    any idea?

  4. Slinger より:

    @Moh
    You need to update the main.xml.

    • 匿名 より:

      You have to create a button in xml file and set id pickTime for that button.

  5. abhijeet sengupta より:

    great tutorial !!!!

タイトルとURLをコピーしました