開始時刻と終了時刻をそれぞれクリックしたときに、クリックしたほうにだけ時間を挿入する機能を追加しました

This commit is contained in:
Ekamo-11 2024-06-12 14:35:11 +09:00
parent 2d17a13d3b
commit 895d9ceff5
3 changed files with 36 additions and 11 deletions

View File

@ -15,6 +15,7 @@ public class SetUpActivity extends FragmentActivity
private TextView startTime;
private TextView endTime;
private boolean isStartTimeSelected = true; // デフォルトはstartTimeを選択
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -23,21 +24,37 @@ public class SetUpActivity extends FragmentActivity
startTime = findViewById(R.id.startTime);
endTime = findViewById(R.id.endTime);
startTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isStartTimeSelected = true;
showTimePickerDialog();
}
});
endTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isStartTimeSelected = false;
showTimePickerDialog();
}
});
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String str = String.format(Locale.US, "%d:%d", hourOfDay, minute); // Textviewに保存する形式を設定
String str = String.format(Locale.US, "%d:%d", hourOfDay, minute);
startTime.setText(str);
endTime.setText(str);
if (isStartTimeSelected) { //押した場所を判定して押したほうにだけ挿入する
startTime.setText(str);
} else {
endTime.setText(str);
}
}
public void showTimePickerDialog(View v) {
private void showTimePickerDialog() { // Dialogを表示する
DialogFragment newFragment = new TimePick();
newFragment.show(getSupportFragmentManager(), "timePicker");
}
}
}

View File

@ -14,7 +14,7 @@ public class TimePick extends DialogFragment implements
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
public Dialog onCreateDialog(Bundle savedInstanceState) { // TimePeckerの生成
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

View File

@ -8,6 +8,16 @@
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/toMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="🔙"
android:textSize="50dp"
tools:ignore="RtlCompat" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -79,7 +89,6 @@
android:id="@+id/startTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showTimePickerDialog"
android:autofillHints=""
android:inputType=""
tools:ignore="LabelFor"/>
@ -103,7 +112,6 @@
android:id="@+id/endTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showTimePickerDialog"
android:autofillHints=""
android:inputType=""
tools:ignore="LabelFor"/>