日付や時間をユーザーに入力してもらうために DatePicker や TimePicker を使うと便利です。
API 30
DialogFragment
Pickerの実装についてはAndroid 3.0 以上で DialogFragment を使うことが推奨されています。また、Time Picker はDate Picker と同様に作成できます。
DatePicker
Googleの解説では、Layout の android:onClick で Activity のメソッドが呼ばれ、Picker の DialogFragment が表示されます。
1 2 3 |
<Button ... android:onClick="showTimePickerDialog" /> |
では、設定したデータをどうやって取り出すのか?
Layout での android:onClick をやめてしまうという方法もあるようですが
Stackoverflowによると
Picker の DatePickerDialog.OnDateSetListener を使って、 MainActivity にデータを戻してやればいいようです
DatePick.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//package your.package.name; import android.app.DatePickerDialog; import android.app.Dialog; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.fragment.app.DialogFragment; import java.util.Calendar; public class DatePick extends DialogFragment implements DatePickerDialog.OnDateSetListener{ @Override @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); return new DatePickerDialog(getActivity(), (MainActivity)getActivity(), year, month, day); } @Override public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) { } } |
元の設定:
return new DatePickerDialog(getActivity(), this, year, month, day);
新しい設定:
return new DatePickerDialog(getActivity(), (MainActivity)getActivity(), year, month, day);
DatePickerDialog() の第2引数が自分自信 this になっているので
これを、MainActivity にしてやります
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
//package com.example.testdatepicker; import androidx.fragment.app.FragmentActivity; import android.os.Bundle; import android.app.DatePickerDialog.OnDateSetListener; import androidx.fragment.app.DialogFragment; import android.view.View; import android.widget.DatePicker; import android.widget.TextView; import java.util.Locale; public class MainActivity extends FragmentActivity implements OnDateSetListener { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { String str = String.format(Locale.US, "%d/%d/%d",year, monthOfYear+1, dayOfMonth); textView.setText( str ); } public void showDatePickerDialog(View v) { DialogFragment newFragment = new DatePick(); newFragment.show(getSupportFragmentManager(), "datePicker"); } } |
DialogFragment にあった
onDateSet() を持ってきてデータを受け取ります
レイアウトです
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/no_date" android:textSize="50sp" android:textColor="#000" android:layout_margin="30dp" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="showDatePickerDialog" android:layout_margin="30dp" android:text="@string/pick_date" /> </LinearLayout > |
リソース
strings.xml
1 2 3 4 5 |
<resources> <string name="app_name">Your App Name</string> <string name="no_date">No Data</string> <string name="pick_date">Pick Date</string> </resources> |
Time Picker
Time Pickerも同様に作成できます。
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
//package your.package.name; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.DialogFragment; import android.os.Bundle; import android.app.TimePickerDialog; import android.view.View; import android.widget.TextView; import android.widget.TimePicker; import java.util.Locale; public class MainActivity extends FragmentActivity implements TimePickerDialog.OnTimeSetListener { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); } @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { String str = String.format(Locale.US, "%d:%d", hourOfDay, minute); textView.setText( str ); } public void showTimePickerDialog(View v) { DialogFragment newFragment = new TimePick(); newFragment.show(getSupportFragmentManager(), "timePicker"); } } |
TimePick.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
//package your.package.name; import androidx.annotation.NonNull; import androidx.fragment.app.DialogFragment; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.widget.TimePicker; import java.util.Calendar; public class TimePick extends DialogFragment implements TimePickerDialog.OnTimeSetListener{ @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); return new TimePickerDialog(getActivity(), (MainActivity)getActivity(), hour, minute, true); } @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/no_data" android:textSize="50sp" android:textColor="#000" android:layout_margin="30dp" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="showTimePickerDialog" android:layout_margin="30dp" android:text="@string/pick_time" /> </LinearLayout > |
strings.xml
1 2 3 4 5 |
<resources> <string name="app_name">Your App Name</string> <string name="no_data">"No Data"</string> <string name="pick_time">Pick Time</string> </resources> |
関連ページ:
NumberPicker
Reference:
Pickers