Hatice Sarp
09/08/2021, 6:21 AMBasicInputText
for change password. I use focusRequester
to focus with BasicInputText. When I enter the screen, the last textfield is focused and the cursor is in that textfield. How can I change it to put it in the first textfield?Zach Klippenstein (he/him) [MOD]
09/08/2021, 3:00 PMHatice Sarp
09/09/2021, 4:43 AMvar oldPasswordText by remember { mutableStateOf(TextFieldValue()) }
var passwordText by remember { mutableStateOf(TextFieldValue()) }
var passwordAgainText by remember { mutableStateOf(TextFieldValue()) }
InputText(
modifier = Modifier.fillMaxWidth(),
textFieldValue = oldPasswordText,
borderColor = Color.White ,
onTextChanged = { oldPasswordTextState = it },
placeholder = "Old password",
visualTransformation = PasswordVisualTransformation(),
keyboardType = KeyboardType.Password
)
Spacer(modifier = Modifier.height(2.dp))
InputText(
modifier = Modifier.fillMaxWidth(),
textFieldValue = passwordText,
borderColor = Color.White,
onTextChanged = { passwordTextState = it },
placeholder = "New password",
visualTransformation = PasswordVisualTransformation(),
keyboardType = KeyboardType.Password
)
Spacer(modifier = Modifier.height(2.dp))
InputText(
modifier = Modifier.fillMaxWidth(),
textFieldValue = passwordAgain,
borderColor = Color.White,
onTextChanged = { passwordAgainText = it },
placeholder = "New password again",
visualTransformation = PasswordVisualTransformation(),
keyboardType = KeyboardType.Password
)
Hatice Sarp
09/09/2021, 4:44 AM@Composable
fun InputText(
modifier: Modifier = Modifier,
onTextChanged: (TextFieldValue) -> Unit,
textFieldValue: TextFieldValue,
visualTransformation: VisualTransformation = VisualTransformation.None,
) {
val focusRequester = FocusRequester()
LaunchedEffect(key1 = Unit) {
focusRequester.requestFocus()
}
var visualTrans by remember { mutableStateOf(visualTransformation) }
Box(
modifier = modifier
.border(
width = 2.dp,
shape = RoundedCornerShape(8.dp)
)
.clickable(onClick = { focusRequester.requestFocus() })
) {
BasicTextField(
value = textFieldValue,
onValueChange = onTextChanged,
modifier = modifier
.padding(16.dp)
.focusRequester(focusRequester),
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Send
),
visualTransformation = visualTrans,
maxLines = 1,
cursorBrush = SolidColor(Color.White),
textStyle = MaterialTheme.typography.body1.copy(color = Color.Black)
)
}
Zach Klippenstein (he/him) [MOD]
09/09/2021, 4:31 PMInputText
that is composed is the last one to request focus, and that's why you're seeing your last field getting initial focus.
This isn't really how FocusRequester
is meant to be used, afaik. InputText
shouldn't have any FocusRequester
stuff itself, that should all be done by the surrounding composable. I'll post a snippet of what i mean in a secZach Klippenstein (he/him) [MOD]
09/09/2021, 4:42 PMFocusRequester
should be remembered
• In InputText
, the incoming modifier
should only be passed to the outer-most component – you were passing it to both the outer Box
and the inner BasicTextField
• Removed the explicit focus request on click from the outer Box
, since BasicTextField
should already focus itself when clicked.
• Removed the outer Box
entirely since the border modifier can just be passed directly to BasicTextField
.Hatice Sarp
09/12/2021, 6:35 PM