我需要显示错误消息OutlinedTextField,但没有找到任何有关如何执行此操作的文档。我在教程中找到了几种方法,例如使用提示创建自定义输入字段或Text在输入字段下方创建,但它们很旧,也许有更好的方法。我需要显示这样的错误消息:
代码:
@Composable
fun EmailInputField(value: MutableState<String>, state: AuthState) {
OutlinedTextField(
value = value.value,
onValueChange = { value.value = it },
modifier = Modifier.fillMaxWidth(1f).height(60.dp),
textStyle = TextStyle(color = Color.White),
label = { Text(text = "Email", color = Color.White) },
colors = TextFieldDefaults.outlinedTextFieldColors(
focusedBorderColor = blue,
unfocusedBorderColor = Color.White
),
isError = state is AuthState.ValidationError,
singleLine = true
)
}
Run Code Online (Sandbox Code Playgroud) android kotlin android-jetpack-compose android-compose-textfield
我正在尝试设计一个像谷歌搜索栏一样高度降低的搜索栏。但输入文本和占位符文本也被裁剪。
TextField(
value = searchText.value,
singleLine = true,
onValueChange = {
searchText.value = it
},
placeholder = { //placeholder text is also getting cropped
Text(
text = "Search",
fontSize = 20.sp,
)
},
textStyle = TextStyle(fontSize = 20.sp), // input text is getting cropped
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 10.dp)
.height(45.dp), // Here I have decreased the height
shape = RoundedCornerShape(22.dp),
)
Run Code Online (Sandbox Code Playgroud)
我的输入文本和占位符文本被裁剪 50%。怎么解决呢?
由于某种原因,Compose TextField 的点击侦听器对我不起作用。
@Composable
private fun ExposedDropdown(
modifier: Modifier,
list: List<String>,
priority: Int
) {
var expanded by remember { mutableStateOf(false) }
Column(modifier) {
OutlinedTextField(
value = list[priority],
onValueChange = { },
readOnly = true,
singleLine = true,
label = { Text(stringResource(id = R.string.status)) },
modifier = Modifier
.fillMaxWidth()
.clickable { Timber.i("Not working :(") }
.onFocusChanged { if (it.isFocused) expanded = !expanded },
trailingIcon = {
Icon(
imageVector = Icons.Outlined.ArrowDropDown,
contentDescription = null,
modifier = Modifier
.clickable { expanded = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用TextField()Jetpack Compose。我希望文本颜色为白色。
我发现这有效:
ProvideTextStyle(TextStyle(color = Color.White)) {
TextField(
...
)
}
Run Code Online (Sandbox Code Playgroud)
但是,我想在主题级别覆盖它,这样我就不需要重复编写ProvideTextStyle. 我看到MaterialTheme只接受以下参数:
@Composable
fun MaterialTheme(
colors: Colors = MaterialTheme.colors,
typography: Typography = MaterialTheme.typography,
shapes: Shapes = MaterialTheme.shapes,
content: @Composable () -> Unit
)
Run Code Online (Sandbox Code Playgroud)
所以我不知道该怎么做。有人可以帮忙吗?
(撰写版本 = 1.0.0-alpha11)
android android-layout android-jetpack-compose android-compose-textfield android-jetpack-compose-text
我正在尝试为 Compose TextField 实现 onClick 处理程序。当前功能是处理文本字段单击,但禁用手动编辑字段。对于我的用例,我想处理点击并执行其他操作。我想保持 TextField 的外观和感觉,并希望焦点动画也能发生。
readOnly 属性为我提供了从 UX 角度来看我想要的内容,但是当我单击 TextField 时,不会调用 onClick 处理程序。
TextField(
value = text,
onValueChange = { text = it},
readOnly = true,
modifier = Modifier
.clickable(onClick = {
Log.i("TextField", "Clicked")
})
)
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用pointerInput,我怎么也遇到同样的问题。
TextField(
value = text,
onValueChange = { text = it},
readOnly = true,
modifier = Modifier
.pointerInput(Unit) {
detectTapGestures(onTap = {
Log.i("TextField", "Clicked")
}
}
)
Run Code Online (Sandbox Code Playgroud)
由于 Compose 如此之新,很难判断这是一个错误还是有意为之。
android kotlin android-jetpack-compose android-compose-textfield
我正在使用 Jetpack Compose TextField,我想在用户按下操作按钮(imeActionPerformed参数)时关闭虚拟键盘。
val text = +state { "" }
TextField(
value = text.value,
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Done,
onImeActionPerformed = {
// TODO Close the virtual keyboard here <<<
}
onValueChange = { s -> text.value = s }
)
Run Code Online (Sandbox Code Playgroud) android android-input-method kotlin android-jetpack-compose android-compose-textfield
我正在寻找一种使用 TextField Composable 禁用键盘自动建议的方法。
在大约 4 个月前的 Android 时代,使用 EditText 您可以执行类似的操作,将 inputType 设置为textNoSuggestions|textVisiblePassword。
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions|textVisiblePassword" />
Run Code Online (Sandbox Code Playgroud)
我在这里使用两种 inputTypes,因为并非所有键盘都支持该textNoSuggestions字段。
有没有办法用 Jetpack Compose 来做到这一点TextField?我没有看到他们有任何KeyboardOptions模仿此功能的东西。
android android-jetpack-compose android-compose-textfield android-jetpack-compose-text
我想要这样的东西:
\n\xe2\x80\xa2 Hey this is my first paragraph.\n\xe2\x80\xa2 Hey this is my second paragraph.\n And this is the second line.\n\xe2\x80\xa2 Hey this is my third paragraph.\nRun Code Online (Sandbox Code Playgroud)\n 在 Jetpack Compose 中,使用 时Text,我们可以使用 使文本居中TextAlign:
Text(
text = "How many cars are in the garage",
textAlign = TextAlign.Center
)
Run Code Online (Sandbox Code Playgroud)
但是,如果我们想要获取文本可组合项中的点击位置,我们可以使用ClickableText:
ClickableText(
text = AnnotatedString("Click Me"),
onClick = { offset ->
Log.d("ClickableText", "$offset -th character is clicked.")
}
)
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何将 ClickableText 中的文本居中?我可以使用重力,但这只会使组件居中,而不会使其内部的文本居中。
使用 XML 创建 UI 时。有一个密码字段选项,其中密码对用户可见。开发人员所要做的就是设置inputType = TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
在 Jetpack Compose 中,可以选择创建 textField()。然后传入visualTransformation = PasswordVisualTransformation()让打字变成点。然而,它不会像 XML 那样预览字母几秒钟,然后才变成点。
想知道是否有一个等效的 jetpack 撰写密码字段的功能,其中密码在变成点之前对用户可见几秒钟。
谢谢
android kotlin android-jetpack-compose android-compose-textfield