我正在使用Jetpack编写1.0.0-alpha07
。我制作了一个登录屏幕,其中包含两个使用其他可组合功能定制的TextField
。
但是,在ImeAction
中设置keyboardOptions
似乎不起作用。例如,ImeAction.Next
不会将焦点移到下一个TextField。我认为我应该做一些事情,使之成为可能,但没有任何文档或文章曾经简短地谈论过ImeOptions
。下面是我为屏幕编写的代码:
Login可组合:
EmailEdit(onChange = { email.value = it })
PasswordEdit(onChange = { password.value = it })
EmailEdit
@Composable
fun EmailEdit(onChange: (String) -> Unit) {
val t = remember { mutableStateOf("") }
TextField(
value = t.value,
onValueChange = { value ->
t.value = value
onChange(value)
},
leadingIcon = { Icon(asset = Icons.Default.Email) },
label = { Text(text = "Email") },
maxLines = 1,
keyboardOptions = KeyboardOptions(
imeAction = ImeAction.Next, // ** Go to next **
keyboardType = KeyboardType.Email
),
visualTransformation = VisualTransformation.None
)
} errorHint = "Not a valid email"
)
}
PassEdit
@Composable
fun PasswordEdit(onChange: (String) -> Unit) {
val t = remember { mutableStateOf("") }
TextField(
value = t.value,
onValueChange = { value ->
t.value = value
onChange(value)
},
leadingIcon = { Icon(asset = Icons.Default.Security) },
label = { Text(text = "Password") },
maxLines = 1,
keyboardOptions = KeyboardOptions(
imeAction = ImeAction.Done, // ** Done. Close the keyboard **
keyboardType = KeyboardType.Text
),
visualTransformation = PasswordVisualTransformation()
)
}
要执行Done
和Next
,我应该添加哪些代码?
发布于 2021-03-21 08:48:26
您可以使用
包含配置的
keyboardOptions
:软件键盘选项,如KeyboardType
和ImeAction
keyboardActions
,当输入服务发出IME操作时,相应的回调称为。
对于Done
您可以使用与键盘进行交互。
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(
onDone = {keyboardController?.hide()}
)
对于Next
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
keyboardActions = KeyboardActions(
onNext = { focusRequester.requestFocus() }
)
类似于:
val (focusRequester) = FocusRequester.createRefs()
val keyboardController = LocalSoftwareKeyboardController.current
TextField(
value = text,
onValueChange = {
text = it
},
label = { Text("Label") },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
keyboardActions = KeyboardActions(
onNext = { focusRequester.requestFocus() }
)
)
TextField(
value = text,
onValueChange = {
text = it
},
modifier = Modifier.focusRequester(focusRequester),
label = { Text("Label") },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(
onDone = { keyboardController?.hide() }
)
)
发布于 2021-08-11 13:31:02
您可以使用LocalFocusManager.
val localFocusManager = LocalFocusManager.current
在您的字段可组合的父级内部。
要将焦点移到下一个字段:
localFocusManager.moveFocus(FocusDirection.Down)
在onNext of KeyboardActions中将焦点移动到特定的方向,如左、右、上、下。
为了明确重点:
localFocusManager.clearFocus()
在KeyboardActions的onDone内部清除焦点。
电子邮件字段:
OutlinedTextField(
value = userId,
onValueChange = { userId = it },
label = { Text("Email") },
modifier = Modifier.fillMaxWidth(),
singleLine = true,
leadingIcon = {
Icon(
painter = painterResource(id = R.drawable.ic_account),
contentDescription = "ID"
)
},
colors = TextFieldDefaults.outlinedTextFieldColors(
focusedBorderColor = Color.Gray,
unfocusedBorderColor = Color.LightGray,
focusedLabelColor = Color(0xffcc0000)
),
keyboardOptions =
KeyboardOptions(
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(onNext = {
localFocusManager.moveFocus(FocusDirection.Down)
})
)
密码字段:
OutlinedTextField(
value = password,
onValueChange = { password = it },
label = { Text("Password") },
modifier = Modifier.fillMaxWidth(),
singleLine = true,
leadingIcon = {
Icon(
painter = painterResource(id = R.drawable.ic_password),
contentDescription = "Password"
)
},
colors = TextFieldDefaults.outlinedTextFieldColors(
focusedBorderColor = Color.Gray,
unfocusedBorderColor = Color.LightGray,
focusedLabelColor = Color(0xffcc0000)
),
keyboardOptions =
KeyboardOptions(
keyboardType = KeyboardType.Password,
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = {
localFocusManager.clearFocus()
})
)
试用1.0.1
版本
发布于 2020-11-22 08:40:46
使用onImeActionPerformed
参数。
对于Done
TextField(
onImeActionPerformed = { _, controller ->
controller?.hideSoftwareKeyboard()
}
)
对于Next
val focusRequester = remember { FocusRequester() }
TextField(
onImeActionPerformed = { _, _ ->
focusRequester.requestFocus()
}
)
TextField(
modifier = Mofifier.focusRequester(focusRequester)
)
下面是一个有用的例子:
val focusRequester = remember { FocusRequester() }
val email = remember { mutableStateOf("") }
TextField(
value = email.value,
onValueChange = { email.value = it },
imeAction = ImeAction.Next,
onImeActionPerformed = { _, _ -> focusRequester.requestFocus() }
)
val password = remember { mutableStateOf("") }
TextField(
value = password.value,
onValueChange = { password.value = it },
imeAction = ImeAction.Done,
onImeActionPerformed = { _, controller -> controller?.hideSoftwareKeyboard() },
modifier = Modifier.focusRequester(focusRequester)
)
文档:
https://stackoverflow.com/questions/64947249
复制相似问题