datePicker #40

Merged
Utahshi merged 11 commits from datePicker into master 2024-07-08 01:21:12 +00:00
3 changed files with 153 additions and 95 deletions

View File

@ -10,6 +10,7 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Paint; import android.graphics.Paint;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.widget.Button; import android.widget.Button;
import android.widget.DatePicker; import android.widget.DatePicker;
@ -24,6 +25,7 @@ import androidx.fragment.app.FragmentActivity;
import androidx.room.Room; import androidx.room.Room;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -42,15 +44,15 @@ public class SetUpActivity extends FragmentActivity
String startBreakTime; String startBreakTime;
String endBreakTime; String endBreakTime;
int totalStudent; int totalStudent;
private TextView setTeacherName; private TextView textViewTeacherName;
private TextView setStartPoint; private TextView textViewStartPoint;
private TextView setStartTime; private TextView textViewStartTime;
private TextView setEndTime; private TextView textViewEndTime;
private TextView setStartBreakTime; private TextView textViewStartBreakTime;
private TextView setEndBreakTime; private TextView textViewEndBreakTime;
private TextView setTotalStudent; private TextView textViewTotalStudent;
private int isDateSelected; private int intIsDateSelected;
private int isStartTimeSelected; private int intIsStartTimeSelected;
String stringYear; String stringYear;
String stringMonth; String stringMonth;
@ -60,11 +62,11 @@ public class SetUpActivity extends FragmentActivity
String stringHourOfDay; String stringHourOfDay;
String stringMinute; String stringMinute;
Button setFirstDay; Button buttonFirstDay;
Button setSecondDay; Button buttonSecondDay;
Button setThirdDay; Button buttonThirdDay;
Button setStartTimeButton; Button buttonStartTimeButton;
Button setEndTimeButton; Button buttonEndTimeButton;
@ -76,28 +78,28 @@ public class SetUpActivity extends FragmentActivity
int classId= getIntent().getIntExtra("classId", 100000); int classId= getIntent().getIntExtra("classId", 100000);
setTeacherName = findViewById(R.id.teacherName); //先生の名前 textViewTeacherName = findViewById(R.id.teacherName); //先生の名前
setStartPoint = findViewById(R.id.startPoint); //開始地点 textViewStartPoint = findViewById(R.id.startPoint); //開始地点
setFirstDay = findViewById(R.id.setFirstDayButton); //1日目の日付 buttonFirstDay = findViewById(R.id.setFirstDayButton); //1日目の日付
setSecondDay = findViewById(R.id.setSecondDayButton); //2日目の日付 buttonSecondDay = findViewById(R.id.setSecondDayButton); //2日目の日付
setThirdDay = findViewById(R.id.setThirdDayButton); //3日目の日付 buttonThirdDay = findViewById(R.id.setThirdDayButton); //3日目の日付
setStartTimeButton = findViewById(R.id.startTimeSetButton); //開始時刻を設定するボタン buttonStartTimeButton = findViewById(R.id.startTimeSetButton); //開始時刻を設定するボタン
setStartTime = findViewById(R.id.startTime); //開始時刻を出力するTextView textViewStartTime = findViewById(R.id.startTime); //開始時刻を出力するTextView
setEndTimeButton = findViewById(R.id.endTimeSetButton); //終了時刻を設定するボタン buttonEndTimeButton = findViewById(R.id.endTimeSetButton); //終了時刻を設定するボタン
setEndTime = findViewById(R.id.endTime); //終了時刻を出力するTextView textViewEndTime = findViewById(R.id.endTime); //終了時刻を出力するTextView
RadioButton setTenMinute = findViewById(R.id.tenMinute); //訪問間隔10分 RadioButton radioButtonTenMinute = findViewById(R.id.tenMinute); //訪問間隔10分
RadioButton setFifteenMinute = findViewById(R.id.fifteenMinute); //訪問間隔15分 RadioButton radioButtonFifteenMinute = findViewById(R.id.fifteenMinute); //訪問間隔15分
RadioButton setThirtyMinute = findViewById(R.id.thirtyMinute); //訪問間隔30分 RadioButton radioButtonThirtyMinute = findViewById(R.id.thirtyMinute); //訪問間隔30分
setStartBreakTime = findViewById(R.id.startBreakTime); //休憩開始時刻 textViewStartBreakTime = findViewById(R.id.startBreakTime); //休憩開始時刻
setStartBreakTime.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG); textViewStartBreakTime.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
setEndBreakTime = findViewById(R.id.endBreakTime); //休憩終了時刻 textViewEndBreakTime = findViewById(R.id.endBreakTime); //休憩終了時刻
setEndBreakTime.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG); textViewEndBreakTime.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
setTotalStudent = findViewById(R.id.totalStudent); //クラスの人数 textViewTotalStudent = findViewById(R.id.totalStudent); //クラスの人数
ImageView toMain = findViewById(R.id.toMain); ImageView toMain = findViewById(R.id.toMain);
Button setUp = findViewById(R.id.setUpButton); //画面下の設定ボタン Button setUp = findViewById(R.id.setUpButton); //画面下の設定ボタン
@ -110,20 +112,20 @@ public class SetUpActivity extends FragmentActivity
setUp.setOnClickListener(view -> { setUp.setOnClickListener(view -> {
teacherName = setTeacherName.getText().toString(); //各変数に値を挿入 teacherName = textViewTeacherName.getText().toString(); //各変数に値を挿入
Log.d(TAG, "Teacher Name: " + teacherName); Log.d(TAG, "Teacher Name: " + teacherName);
startPoint = setStartPoint.getText().toString(); startPoint = textViewStartPoint.getText().toString();
Log.d(TAG, "Start Point: " + startPoint); Log.d(TAG, "Start Point: " + startPoint);
Log.d(TAG, "First Day:" + firstDay); Log.d(TAG, "First Day:" + firstDay);
Log.d(TAG, "Second Day:" + secondDay); Log.d(TAG, "Second Day:" + secondDay);
Log.d(TAG, "Third Day:" + thirdDay); Log.d(TAG, "Third Day:" + thirdDay);
Log.d(TAG, "Start Time" + startTime); Log.d(TAG, "Start Time" + startTime);
Log.d(TAG, "End Time" + endTime); Log.d(TAG, "End Time" + endTime);
if (setTenMinute.isChecked()){ //ラジオボタンの状態を取得 if (radioButtonTenMinute.isChecked()){ //ラジオボタンの状態を取得
intervalTime = "10"; intervalTime = "10";
} else if (setFifteenMinute.isChecked()) { } else if (radioButtonFifteenMinute.isChecked()) {
intervalTime = "15"; intervalTime = "15";
} else if (setThirtyMinute.isChecked()) { } else if (radioButtonThirtyMinute.isChecked()) {
intervalTime = "30"; intervalTime = "30";
} else { } else {
intervalTime = "0"; intervalTime = "0";
@ -131,10 +133,25 @@ public class SetUpActivity extends FragmentActivity
Log.d(TAG, "Interval Time" + intervalTime); Log.d(TAG, "Interval Time" + intervalTime);
Log.d(TAG, "Start Break Time" + startBreakTime); Log.d(TAG, "Start Break Time" + startBreakTime);
Log.d(TAG, "End Break Time" + endBreakTime); Log.d(TAG, "End Break Time" + endBreakTime);
totalStudent = Integer.parseInt(setTotalStudent.getText().toString()); //数値型に変更
Log.d(TAG, "Total Student" + totalStudent); Log.d(TAG, "Total Student" + totalStudent);
Log.d(TAG, "onClick: できてるよ"); Log.d(TAG, "onClick: できてるよ");
// クラスの人数を数値に変換して取得
try {
totalStudent = Integer.parseInt(textViewTotalStudent.getText().toString());
} catch (NumberFormatException e) {
totalStudent = 0; // デフォルト値を設定するかエラー処理を追加することも考慮する必要があります
}
// 入力データのバリデーション
if (TextUtils.isEmpty(teacherName) || TextUtils.isEmpty(startPoint) || TextUtils.isEmpty(startTime)
|| TextUtils.isEmpty(firstDay) || TextUtils.isEmpty(secondDay) || TextUtils.isEmpty(thirdDay)
|| TextUtils.isEmpty(endTime) || Objects.equals(intervalTime, "0") || TextUtils.isEmpty(startBreakTime)
|| TextUtils.isEmpty(endBreakTime) || totalStudent <= 0) {
Toast.makeText(SetUpActivity.this, "必須項目を入力してください", Toast.LENGTH_SHORT).show();
return;
}
// データベースへの登録処理 // データベースへの登録処理
ExecutorService executor = Executors.newSingleThreadExecutor(); ExecutorService executor = Executors.newSingleThreadExecutor();
@ -175,9 +192,10 @@ public class SetUpActivity extends FragmentActivity
SharedPreferences sharedPreferences=getSharedPreferences("visitingDate",MODE_PRIVATE); SharedPreferences sharedPreferences=getSharedPreferences("visitingDate",MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit(); SharedPreferences.Editor editor= sharedPreferences.edit();
editor.putString("day1",firstDay); //editorに値を渡す
editor.putString("day2",secondDay); editor.putString("day1",firstDay); //1日目
editor.putString("day3",thirdDay); editor.putString("day2",secondDay); //2日目
editor.putString("day3",thirdDay); //3日目
editor.apply(); editor.apply();
@ -186,52 +204,71 @@ public class SetUpActivity extends FragmentActivity
}); });
setFirstDay.setOnClickListener(v ->{ //DatePicker用
isDateSelected = 1; buttonFirstDay.setOnClickListener(v ->{
intIsDateSelected = 1; //ボタンの判別Date
showDatePickerDialog(); //DatePickerの表示 showDatePickerDialog(); //DatePickerの表示
}); });
setSecondDay.setOnClickListener(v ->{ buttonSecondDay.setOnClickListener(v ->{
isDateSelected = 2; intIsDateSelected = 2;
showDatePickerDialog(); showDatePickerDialog();
}); });
setThirdDay.setOnClickListener(v ->{ buttonThirdDay.setOnClickListener(v ->{
isDateSelected = 3; intIsDateSelected = 3;
showDatePickerDialog(); showDatePickerDialog();
}); });
setStartTimeButton.setOnClickListener(v -> { //TimePicker用
isStartTimeSelected = 1; //ボタンの判別 buttonStartTimeButton.setOnClickListener(v -> {
showTimePickerDialog(); //TimePickerの表示 intIsStartTimeSelected = 1; //ボタンの判別Time
showTimePickerDialog(); //TimePickerの表示
}); });
setEndTimeButton.setOnClickListener(v -> { buttonEndTimeButton.setOnClickListener(v -> {
isStartTimeSelected = 2; intIsStartTimeSelected = 2;
showTimePickerDialog(); showTimePickerDialog();
}); });
setStartBreakTime.setOnClickListener(v -> { textViewStartBreakTime.setOnClickListener(v -> {
isStartTimeSelected = 3; intIsStartTimeSelected = 3;
showTimePickerDialog(); showTimePickerDialog();
}); });
setEndBreakTime.setOnClickListener(v -> { textViewEndBreakTime.setOnClickListener(v -> {
isStartTimeSelected = 4; intIsStartTimeSelected = 4;
showTimePickerDialog(); showTimePickerDialog();
}); });
//リセットボタンの処理 //リセットボタンの処理
reset.setOnClickListener(v -> { //テキストとラジオボタンの選択を消去 reset.setOnClickListener(v -> { //テキストとラジオボタンの選択を消去
setTeacherName.setText(""); textViewTeacherName.setText("");
setStartPoint.setText(""); textViewStartPoint.setText("");
setTenMinute.setChecked(false); buttonFirstDay.setText("");
setFifteenMinute.setChecked(false); buttonSecondDay.setText("");
setThirtyMinute.setChecked(false); buttonThirdDay.setText("");
setStartBreakTime.setText(""); radioButtonTenMinute.setChecked(false);
setEndBreakTime.setText(""); radioButtonFifteenMinute.setChecked(false);
setTotalStudent.setText(""); radioButtonThirtyMinute.setChecked(false);
textViewStartTime.setText("");
textViewEndTime.setText("");
textViewStartBreakTime.setText("");
textViewEndBreakTime.setText("");
textViewTotalStudent.setText("");
teacherName = "";
startPoint = "";
firstDay = "";
secondDay = "";
thirdDay = "";
startTime = "";
endTime = "";
intervalTime = "";
startBreakTime = "";
endBreakTime ="" ;
ExecutorService executor = Executors.newSingleThreadExecutor(); ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(() -> { executor.execute(() -> {
@ -247,26 +284,27 @@ public class SetUpActivity extends FragmentActivity
// DatePickerDialogで選択された日付を処理する // DatePickerDialogで選択された日付を処理する
String str = String.format(Locale.JAPAN, "%02d/%02d", month + 1, dayOfMonth); // TextViewに表示する日付の形式を設定 String str = String.format(Locale.JAPAN, "%02d/%02d", month + 1, dayOfMonth); // TextViewに表示する日付の形式を設定
if (isDateSelected == 1) { if (intIsDateSelected == 1) {
stringYear = String.valueOf(year); // stringYear = String.valueOf(year); //
stringMonth = String.format(Locale.JAPAN, "%02d", month + 1); // stringMonth = String.format(Locale.JAPAN, "%02d", month + 1); //
stringDayOfMonth = String.format(Locale.JAPAN, "%02d", dayOfMonth); // stringDayOfMonth = String.format(Locale.JAPAN, "%02d", dayOfMonth); //
firstDay = stringYear + stringMonth + stringDayOfMonth; firstDay = stringYear + stringMonth + stringDayOfMonth; //8桁の文字列を作成 )20240604
setFirstDay.setText(str); buttonFirstDay.setText(str); //buttonにformatされた文字列を挿入
} else if (isDateSelected == 2) {
} else if (intIsDateSelected == 2) {
stringYear = String.valueOf(year); stringYear = String.valueOf(year);
stringMonth = String.format(Locale.JAPAN, "%02d", month + 1); stringMonth = String.format(Locale.JAPAN, "%02d", month + 1);
stringDayOfMonth = String.format(Locale.JAPAN, "%02d", dayOfMonth); stringDayOfMonth = String.format(Locale.JAPAN, "%02d", dayOfMonth);
secondDay = stringYear + stringMonth + stringDayOfMonth; secondDay = stringYear + stringMonth + stringDayOfMonth;
setSecondDay.setText(str); buttonSecondDay.setText(str);
} else if (isDateSelected == 3) { } else if (intIsDateSelected == 3) {
stringYear = String.valueOf(year); stringYear = String.valueOf(year);
stringMonth = String.format(Locale.JAPAN, "%02d", month + 1); stringMonth = String.format(Locale.JAPAN, "%02d", month + 1);
stringDayOfMonth = String.format(Locale.JAPAN, "%02d", dayOfMonth); stringDayOfMonth = String.format(Locale.JAPAN, "%02d", dayOfMonth);
thirdDay = stringYear + stringMonth + stringDayOfMonth; thirdDay = stringYear + stringMonth + stringDayOfMonth;
setThirdDay.setText(str); buttonThirdDay.setText(str);
} }
} }
@ -276,38 +314,38 @@ public class SetUpActivity extends FragmentActivity
public void onTimeSet(TimePicker view, int hourOfDay, int minute) { public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String str = String.format(Locale.JAPAN, "%02d:%02d", hourOfDay, minute); // Textviewに保存する形式を設定 String str = String.format(Locale.JAPAN, "%02d:%02d", hourOfDay, minute); // Textviewに保存する形式を設定
if (isStartTimeSelected == 1) { if (intIsStartTimeSelected == 1) {
stringHourOfDay = String.format("%02d", hourOfDay); stringHourOfDay = String.format("%02d", hourOfDay); //
stringMinute = String.format("%02d", minute); stringMinute = String.format("%02d", minute); //
startTime = stringHourOfDay + stringMinute; startTime = stringHourOfDay + stringMinute; //4桁の文字列を作成 0930
setStartTime.setText(str); textViewStartTime.setText(str); //textViewにformatされている文字列を挿入
} else if (isStartTimeSelected == 2) { } else if (intIsStartTimeSelected == 2) {
stringHourOfDay = String.format("%02d", hourOfDay); stringHourOfDay = String.format("%02d", hourOfDay);
stringMinute = String.format("%02d", minute); stringMinute = String.format("%02d", minute);
endTime = stringHourOfDay + stringMinute; endTime = stringHourOfDay + stringMinute;
setEndTime.setText(str); textViewEndTime.setText(str);
} else if (isStartTimeSelected == 3) { } else if (intIsStartTimeSelected == 3) {
stringHourOfDay = String.format("%02d", hourOfDay); stringHourOfDay = String.format("%02d", hourOfDay);
stringMinute = String.format("%02d", minute); stringMinute = String.format("%02d", minute);
startBreakTime =stringHourOfDay + stringMinute; startBreakTime =stringHourOfDay + stringMinute;
setStartBreakTime.setText(" " + str + " "); textViewStartBreakTime.setText(" " + str + " ");
} else if (isStartTimeSelected == 4) { } else if (intIsStartTimeSelected == 4) {
stringHourOfDay = String.format("%02d", hourOfDay); stringHourOfDay = String.format("%02d", hourOfDay);
stringMinute = String.format("%02d", minute); stringMinute = String.format("%02d", minute);
endBreakTime = stringHourOfDay + stringMinute; endBreakTime = stringHourOfDay + stringMinute;
setEndBreakTime.setText(" " + str + " "); textViewEndBreakTime.setText(" " + str + " ");
} }
} }
private void showDatePickerDialog() { private void showDatePickerDialog() { //DatePickerDialogを表示する
DialogFragment newFragment = new DatePick(); DialogFragment newFragment = new DatePick();
newFragment.show(getSupportFragmentManager(), "datePicker"); newFragment.show(getSupportFragmentManager(), "datePicker");
} }
private void showTimePickerDialog() { // Dialogを表示する private void showTimePickerDialog() { // TimePickerDialogを表示する
DialogFragment newFragment = new TimePick(); DialogFragment newFragment = new TimePick();
newFragment.show(getSupportFragmentManager(), "timePicker"); newFragment.show(getSupportFragmentManager(), "timePicker");
} }

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -23,9 +22,10 @@
android:id="@+id/toMain" android:id="@+id/toMain"
android:layout_width="60dp" android:layout_width="60dp"
android:layout_height="60dp" android:layout_height="60dp"
android:layout_gravity="left" android:layout_gravity="start"
android:layout_marginLeft="10dp" android:layout_marginStart="10dp"
android:src="@drawable/back_button"/> android:src="@drawable/back_button"
android:contentDescription="@string/todo" />
<TextView <TextView
@ -52,7 +52,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:autofillHints="" android:autofillHints=""
android:inputType="" android:inputType="text"
tools:ignore="LabelFor" /> tools:ignore="LabelFor" />
</LinearLayout> </LinearLayout>
@ -76,7 +76,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/startpoint" android:hint="@string/startpoint"
android:autofillHints="" android:autofillHints=""
android:inputType="" android:inputType="text"
tools:ignore="LabelFor" /> tools:ignore="LabelFor" />
</LinearLayout> </LinearLayout>
@ -91,7 +91,7 @@
<TextView <TextView
android:layout_width="90sp" android:layout_width="90sp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="日付設定" android:text="@string/setUpDate"
android:gravity="center"/> android:gravity="center"/>
<!-- <TextView--> <!-- <TextView-->
@ -109,7 +109,7 @@
android:id="@+id/setFirstDayButton" android:id="@+id/setFirstDayButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="1日目" android:hint="@string/firstDay"
android:layout_marginStart="7sp" android:layout_marginStart="7sp"
android:layout_marginEnd="7sp" android:layout_marginEnd="7sp"
tools:ignore="DuplicateIds" /> tools:ignore="DuplicateIds" />
@ -118,7 +118,7 @@
android:id="@+id/setSecondDayButton" android:id="@+id/setSecondDayButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="2日目" android:hint="@string/secondDay"
android:layout_marginStart="7sp" android:layout_marginStart="7sp"
android:layout_marginEnd="7sp" android:layout_marginEnd="7sp"
tools:ignore="DuplicateIds" /> tools:ignore="DuplicateIds" />
@ -127,7 +127,7 @@
android:id="@+id/setThirdDayButton" android:id="@+id/setThirdDayButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="3日目" android:hint="@string/threeDay"
android:layout_marginStart="7sp" android:layout_marginStart="7sp"
android:layout_marginEnd="7sp" android:layout_marginEnd="7sp"
tools:ignore="DuplicateIds" /> tools:ignore="DuplicateIds" />
@ -151,7 +151,7 @@
android:id="@+id/startTime" android:id="@+id/startTime"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="未設定" android:hint="@string/notSetUp"
android:textSize="20sp" android:textSize="20sp"
android:textAlignment="center" android:textAlignment="center"
android:autofillHints="" android:autofillHints=""
@ -186,7 +186,7 @@
android:id="@+id/endTime" android:id="@+id/endTime"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="未設定" android:hint="@string/notSetUp"
android:textSize="20sp" android:textSize="20sp"
android:textAlignment="center" android:textAlignment="center"
android:autofillHints="" android:autofillHints=""

View File

@ -16,4 +16,24 @@
<string name="tenMinute">10分</string> <string name="tenMinute">10分</string>
<string name="fifteenMinute">15分</string> <string name="fifteenMinute">15分</string>
<string name="thirtyMinute">30分</string> <string name="thirtyMinute">30分</string>
<string name="todo">TODO</string>
<string name="setUpDate">日付設定</string>
<string name="firstDay">1日目</string>
<string name="secondDay">2日目</string>
<string name="threeDay">3日目</string>
<string name="notSetUp">未設定</string>
<string-array name="months_array">
<item>1月</item>
<item>2月</item>
<item>3月</item>
<item>4月</item>
<item>5月</item>
<item>6月</item>
<item>7月</item>
<item>8月</item>
<item>9月</item>
<item>10月</item>
<item>11月</item>
<item>12月</item>
</string-array>
</resources> </resources>