Skip to content

Commit

Permalink
[TimePicker] Only clear hour/minute fields when a digit is entered
Browse files Browse the repository at this point in the history
Fixes issue with keyboard navigation that unnecessarily clears the text.

PiperOrigin-RevId: 620286833
  • Loading branch information
paulfthomas committed Apr 2, 2024
1 parent 0a21fe7 commit 4f21b95
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ private boolean onMinuteKeyPress(int keyCode, KeyEvent event, EditText editText)
return true;
}

clearPrefilledText(editText);
if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
clearPrefilledText(editText);
}

return false;
}
Expand All @@ -135,7 +137,9 @@ private boolean onHourKeyPress(int keyCode, KeyEvent event, EditText editText) {
return true;
}

clearPrefilledText(editText);
if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
clearPrefilledText(editText);
}

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ public void controller_clearPrefilledText_shouldNotClearWhenPartialText() {
assertThat(editText.getText().length()).isEqualTo(1);
}

@Test
public void controller_clearPrefilledText_shouldNotClearWhenNotDigit() {
EditText editText = hourInput.getTextInput().getEditText();
editText.setText("0");
editText.setSelection(0);
pressKeys(editText, KeyEvent.KEYCODE_DPAD_RIGHT);
shadowOf(getMainLooper()).idle();

assertThat(editText.getText().length()).isEqualTo(1);
}

@Test
public void afterTextChanged_validHourInput_formatsText() {
EditText editText = hourInput.getTextInput().getEditText();
Expand Down

0 comments on commit 4f21b95

Please sign in to comment.