さまよえる、Android

Androidのプログラミングで便利なことや残しておきたいことを残しておく。もしオススメのライブラリがあったら教えてくださいね。

AndroidのUIライブラリ、MaterialDateTimePickerを使ってみる。

Androidで日時をユーザーに選択してもらう時に使うUI。需要はあるけど、実装するのは、面倒だよね。こういうのはライブラリでサクッと使いたいところだ。

github.com

https://camo.githubusercontent.com/1467c2b5d209d68aef8ac96cc6bdc28a6c2b1f3d/68747470733a2f2f7261772e6769746875622e636f6d2f7764756c6c6165722f4d6174657269616c4461746554696d655069636b65722f67682d70616765732f696d616765732f646174655f7069636b65722e706e67

https://camo.githubusercontent.com/968ac1b84bfe4514fabf2c25c94a1381d3af3973/68747470733a2f2f7261772e6769746875622e636f6d2f7764756c6c6165722f4d6174657269616c4461746554696d655069636b65722f67682d70616765732f696d616765732f74696d655f7069636b65722e706e67

    compile 'com.wdullaer:materialdatetimepicker:1.5.4'

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
import com.wdullaer.materialdatetimepicker.time.RadialPickerLayout;
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener,
        DatePickerDialog.OnDateSetListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showTimePickerDialog();
        //showDatePickerDialog();
    }

    public void showTimePickerDialog() {
        Calendar now = Calendar.getInstance();
        TimePickerDialog tpd = TimePickerDialog.newInstance(
                MainActivity.this,
                now.get(Calendar.HOUR_OF_DAY),
                now.get(Calendar.MINUTE),
                false
        );
        tpd.show(getFragmentManager(), "Timepickerdialog");

    }

    public void showDatePickerDialog() {
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(
                MainActivity.this,
                now.get(Calendar.YEAR),
                now.get(Calendar.MONTH),
                now.get(Calendar.DAY_OF_MONTH)
        );
        dpd.show(getFragmentManager(), "Datepickerdialog");
    }
    @Override
    public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
        String time = hourOfDay+"時:"+minute + "分";
        Toast.makeText(this, time, Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
        String date = year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日";
        Toast.makeText(this, date, Toast.LENGTH_LONG).show();

    }
}

showTimePickerDialog

時間指定

showDatePickerDialog

日時指定

年月日のMAXを設定する

JavaのCalendarクラスを使って設定できる。2015年5月5日以降は選択不能にする場合は下記の通り。

 Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(
                MainActivity.this,
                now.get(Calendar.YEAR),
                now.get(Calendar.MONTH),
                now.get(Calendar.DAY_OF_MONTH)
        );
        dpd.setThemeDark(true);
        Calendar calendar = Calendar.getInstance(); // Calendar取得
        calendar.set(Calendar.YEAR, 2015);
        calendar.set(Calendar.DAY_OF_MONTH,5);
        calendar.set(Calendar.MONTH,5 - 1);

        dpd.setMaxDate(calendar);
        dpd.show(getFragmentManager(), "Datepickerdialog");

setMinDate、setMaxDateで選択不能にする年月日を指定できるみたい。