我正在制作一个待办事项应用程序,当我编写待办事项时,我只放置了 1 行,但是当我单击 Enter 时,它会创建一个新行,有什么方法可以修复它吗?
@Composable
fun TextFieldDemo() {
Column(
Modifier
.padding(50.dp, 600.dp, 0.dp, 0.dp)
.fillMaxHeight()) {
val textState = remember { mutableStateOf(TextFieldValue()) }
TextField(
value = textState.value,
onValueChange = { textState.value = it },
label = {Text(text = "What you need Todo?")},
singleLine = true //apenas uma linha de texto , podendo usar-se tambem singleLine = true
)
}
}
Run Code Online (Sandbox Code Playgroud) 我想TextField用 3 行来创建:
即使其中没有任何文本TextField,我也想看到 3 行,即我需要经典 xml 布局中的直接等效项EditText.lines。
我的不起作用的代码是:
OutlinedTextField(
value = currentText,
onValueChange = { currentText = it },
label = { Text ("Label") },
maxLines = 3,
modifier = Modifier.fillMaxWidth().wrapContentHeight().padding(16.dp),
singleLine = false
)
Run Code Online (Sandbox Code Playgroud)
你可以帮帮我吗?
这是一个可组合函数。当我单击 CardView 时,文章应该在 Android 视图中打开。我不知道如何实现这一目标。
我也用 lambda 尝试过,但没有成功。
我想ShowOnWebView在NewsCardView(Card View)单击函数时调用可组合函数,但编译器显示错误:
@Composable 调用只能在 @Composable 函数的上下文中发生
单击 CardView 时如何调用我的函数?
@Composable
fun NewsCardView(
article: Article,
modifier: Modifier = Modifier,
) {
Card(
modifier = modifier
.height(150.dp)
.fillMaxWidth()
.padding(2.dp)
.clickable {
// I want to call ShowOnWebView here
},
elevation = 5.dp,
backgroundColor = Color.White
) {
val painter = rememberImagePainter(data = article.urlToImage) {
crossfade(1000)
error(R.drawable.ic_placeholder)
placeholder(R.drawable.ic_placeholder)
}
Row(
modifier = Modifier
.fillMaxSize(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceEvenly
) …Run Code Online (Sandbox Code Playgroud) android kotlin android-jetpack android-jetpack-compose android-compose-textfield
我有一个可以在任何地方Jetpack Compose使用的应用程序stringResource,并且非常适合localization.
但是,我有一些不是Composable函数的函数,我想从resources. 当我尝试stringResource在这些函数中使用时,它告诉我它只能在可组合项中使用。
那么,如果我不是可组合函数,如何从资源中获取字符串?
我想创建textfield与hint文本jetpackcompose。任何示例如何创建textfield使用jectpack?谢谢
android android-textinputlayout android-jetpack-compose android-compose-textfield
嗨,我需要删除我的 TextField 中的下划线,因为当 TextField 是圆形时它看起来很难看。我已经将 activeColor 设置为透明,但是光标不会显示(因为它是透明的)。如何删除下划线/activeColor 并保留光标?
这是我的循环文本字段代码:
@Composable
fun SearchBar(value: String) {
// we are creating a variable for
// getting a value of our text field.
val inputvalue = remember { mutableStateOf(TextFieldValue()) }
TextField(
// below line is used to get
// value of text field,
value = inputvalue.value,
// below line is used to get value in text field
// on value change in text field.
onValueChange = { inputvalue.value = it },
// below line …Run Code Online (Sandbox Code Playgroud) android android-textinputlayout android-jetpack-compose android-compose-textfield android-jetpack-compose-text
我一直在尝试 Jetpack compose,我注意到,尽管Text切换到深色模式时文本颜色会正确更新,但TextField或 的文本颜色OutlinedTextField仍然顽固地保持黑色。不过,标签和提示的颜色正确。
确定字段的默认文本样式是MaterialTheme.typography.body1我已更新我的应用程序主题以包含此解决方法:
val typography = if (darkTheme) {
//TODO: Hack to show text field text in dark mode
MaterialTheme.typography.copy(body1 = MaterialTheme.typography.body1.copy(color = Color.White))
} else {
MaterialTheme.typography
}
MaterialTheme(colors = colors, content = content, typography = typography)
Run Code Online (Sandbox Code Playgroud)
但如果这是解决方案,我就必须对每种排版风格都这样做,而且感觉应该是自动的。那么我是否做错了什么,或者这是在正式发布之前将得到解决的问题之一?
这是我的实际可组合项之一(包含在我的主题中):
@Composable
fun UsernameField(
value: String,
isError: Boolean,
onValueChange: (String) -> Unit,
) {
Column {
OutlinedTextField(
value = value,
onValueChange = onValueChange,
modifier = Modifier.fillMaxWidth(),
label = { Text("Username") }, …Run Code Online (Sandbox Code Playgroud) android android-jetpack-compose android-darkmode android-compose-textfield
我只想在文本字段中输入数字。我尝试使用此stackoverflow逻辑来限制字母和特殊字符,但是当我按键盘上的点时,它会崩溃。
错误
2022-08-18 09:47:13.966 8050-8050/com.abc.app.dev E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.abc.app.dev, PID: 8050
java.lang.NumberFormatException: For input string: "."
at java.lang.Integer.parseInt(Integer.java:733)
at java.lang.Integer.parseInt(Integer.java:865)
at com.abc.app.yo.composable.InputKt$InputWithUnitContainer$1$1$1$1.invoke(Input.kt:187)
at com.abc.app.yo.composable.InputKt$InputWithUnitContainer$1$1$1$1.invoke(Input.kt:186)
at androidx.compose.foundation.text.BasicTextFieldKt$BasicTextField$7$1.invoke(BasicTextField.kt:266)
at androidx.compose.foundation.text.BasicTextFieldKt$BasicTextField$7$1.invoke(BasicTextField.kt:264)
at androidx.compose.foundation.text.CoreTextFieldKt$CoreTextField$onValueChangeWrapper$1.invoke(CoreTextField.kt:241)
at androidx.compose.foundation.text.CoreTextFieldKt$CoreTextField$onValueChangeWrapper$1.invoke(CoreTextField.kt:236)
at androidx.compose.foundation.text.TextFieldDelegate$Companion.onEditCommand(TextFieldDelegate.kt:198)
at androidx.compose.foundation.text.TextFieldDelegate$Companion.access$onEditCommand(TextFieldDelegate.kt:90)
at androidx.compose.foundation.text.TextFieldDelegate$Companion$restartInput$1.invoke(TextFieldDelegate.kt:246)
at androidx.compose.foundation.text.TextFieldDelegate$Companion$restartInput$1.invoke(TextFieldDelegate.kt:243)
at androidx.compose.ui.text.input.TextInputServiceAndroid$createInputConnection$1.onEditCommands(TextInputServiceAndroid.android.kt:111)
at androidx.compose.ui.text.input.RecordingInputConnection.endBatchEditInternal(RecordingInputConnection.android.kt:162)
at androidx.compose.ui.text.input.RecordingInputConnection.addEditCommandWithBatch(RecordingInputConnection.android.kt:136)
at androidx.compose.ui.text.input.RecordingInputConnection.commitText(RecordingInputConnection.android.kt:181)
at com.android.internal.inputmethod.RemoteInputConnectionImpl.lambda$commitText$16$com-android-internal-inputmethod-RemoteInputConnectionImpl(RemoteInputConnectionImpl.java:569)
at com.android.internal.inputmethod.RemoteInputConnectionImpl$$ExternalSyntheticLambda34.run(Unknown Source:8)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Run Code Online (Sandbox Code Playgroud)
代码
appendTextFieldValue: (TextFieldValue) -> Unit,
Run Code Online (Sandbox Code Playgroud)
这是传递函数,下面是我的文本字段 …
android kotlin android-jetpack android-jetpack-compose android-compose-textfield
我正在尝试在我的 Compose 应用程序中实现搜索栏。我有一个文本字段,当我在文本字段外部单击时,我想失去焦点并隐藏键盘。但是,我不知道该怎么做。我确实尝试过这样做,我在 GitHub 上看到了这一点。
不过,我做了一些改变。原始解决方案使用了这部分代码 isHintDisplayed = it != FocusState.Active,但似乎已被弃用。所以我把它改成了isHintDisplayed = it.isFocused != true理论上应该做同样的事情。如果我恢复到旧版本的 compose 并使用 FocusState.Active,焦点/取消焦点可以完美工作,但我无法让它与我的代码一起使用。
有任何想法吗?
这是我的代码:
@Composable
fun ListScreen(
navController: NavController
) {
Surface(
color = MaterialTheme.colors.background,
modifier = Modifier.fillMaxSize()
) {
Column {
Spacer(modifier = Modifier.height(20.dp))
Image(
painter = painterResource(id = R.drawable.ic_international_pok_mon_logo),
contentDescription = "Cards",
modifier = Modifier
.fillMaxWidth()
.align(CenterHorizontally)
)
SearchBar(
hint = "Search",
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
) {
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是搜索栏:
@Composable
fun SearchBar(
modifier: Modifier …Run Code Online (Sandbox Code Playgroud) android kotlin android-jetpack-compose android-compose-textfield
compose_version = 1.2.0
kotlin 1.7.0
Run Code Online (Sandbox Code Playgroud)
我正在尝试删除TextField. 因为我将它与尾随图标一起使用。所以我不能使用它,BasicTextField因为它没有尾随图标。
我正在使用版本1.2.0,我认为includeFontPadding默认情况下是错误的。然而,这没有用。所以我尝试明确地尝试将其设置如下:
textStyle = TextStyle(
platformStyle = PlatformTextStyle(
includeFontPadding = true
))
Run Code Online (Sandbox Code Playgroud)
然而,这也不起作用。所以只是想知道 1.2.0 版本并删除默认填充。
Column(modifier = Modifier
.fillMaxWidth()
.background(color = Color.White)
.border(width = 2.dp, shape = RoundedCornerShape(4.dp), color = Color.LightGray)
.height(56.dp)) {
Text(
modifier = Modifier
.fillMaxWidth()
.padding(start = 16.dp),
text = "Gender (optional)")
TextField(
textStyle = TextStyle(
platformStyle = PlatformTextStyle(
includeFontPadding = true
)),
colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.White),
modifier = Modifier
.fillMaxWidth(),
value …Run Code Online (Sandbox Code Playgroud)