화면 기록 2024-11-13 오전 12.20.05.mov
화면 기록 2024-11-13 오전 12.29.39.mov
위와 같은 UX는 추후 무조건 개선해야 한다 생각이 들었고, 개선을 미루기 보다는 바로 해결방법을 찾아보았습니다. 그러던 중 자동으로 다음 텍스트 필드로 포커스를 이동하게 할 수 있는 방식
을 발견하고 적용하게 되었습니다.
위 동작을 하기 위해서는 keboardOptions
, keyboardActions
를 지정해줘야 하는데요. 따라서 공통 컴포넌트에 다음과 같이 keyboardOptions
, keyboardActions
property를 추가해줬습니다.
(default 값을 지정해주었기 때문에 기존 구현도 오류 없이 동작합니다!)
@Composable
fun WeQuizTextField(
modifier: Modifier = Modifier,
label: String,
text: String,
onTextChanged: (String) -> Unit,
placeholder: String,
minLines: Int = 1,
maxLines: Int = 1,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default, // 추가
keyboardActions: KeyboardActions = KeyboardActions.Default,
) {
TextField(
value = text,
onValueChange = { onTextChanged(it) },
modifier = modifier.fillMaxWidth(),
textStyle = MaterialTheme.typography.bodyLarge,
label = { Text(label) },
placeholder = { Text(placeholder) },
minLines = minLines,
maxLines = maxLines,
trailingIcon = {
IconButton(
onClick = { onTextChanged("") },
) {
Icon(
painter = painterResource(R.drawable.outline_cancel_24),
tint = MaterialTheme.colorScheme.onSurfaceVariant,
contentDescription = stringResource(id = R.string.des_clear_text),
)
}
},
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
)
}
우선 사용을 하기 위해서는 두 가지 상태가 필요합니다.
focusRequester
제일 처음 focus를 둘 textField에 설정해줘야 합니다.focusManager
해당 값으로 이제 focus를 다음으로 이동하거나 focus를 제거할 수 있습니다.WeQuizTextField(
modifier = Modifier.focusRequester(focusRequester),
label = stringResource(id = R.string.txt_question_title_label),
text = title,
onTextChanged = onTitleChanged,
placeholder = stringResource(id = R.string.txt_question_title_placeholder),
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
)