Kotlin jetpack compose 文本输入框ExitText/TextField remember 居然可以传两个参数
Posted 安果移不动
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kotlin jetpack compose 文本输入框ExitText/TextField remember 居然可以传两个参数相关的知识,希望对你有一定的参考价值。
package com.anguomob.jecpack.activity.compose.todo.one
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.*
import androidx.compose.material.SnackbarDefaults.backgroundColor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.databinding.adapters.TextViewBindingAdapter
import com.anguomob.jecpack.activity.compose.todo.bean.TodoItem
@Composable
fun TodoInputText(text: String, onTextChanged: (String) -> Unit, modifier: Modifier = Modifier)
TextField(
value = text,
onValueChange = onTextChanged,
modifier = modifier,
colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.Transparent),
maxLines = 1,
)
@Composable()
fun TodoEditButton(
onClick: () -> Unit,
text: String,
modifier: Modifier = Modifier,
enable: Boolean = true
)
TextButton(
onClick = onClick,
shape = CircleShape,
colors = ButtonDefaults.buttonColors(),
modifier = modifier,
enabled = enable
)
Text(text)
@Composable
fun TodoItemInput(onItemComplete: (TodoItem) -> Unit)
val (text, setText) = remember
mutableStateOf("")
Column
Row(
Modifier
.padding(horizontal = 16.dp)
.padding(top = 16.dp)
)
TodoInputText(
text = text,
modifier = Modifier
.weight(1f)
.padding(end = 8.dp),
onTextChanged = setText
)
TodoEditButton(
onClick =
onItemComplete(TodoItem(text))
setText("")
,
text = "Add",
modifier = Modifier.align(Alignment.CenterVertically),
enable = !text.isBlank()
)
使用
open class ComposeActivity : ComponentActivity()
val todoViewModel: TodoViewModel by viewModels()
private val TAG = "ComposeActivity"
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContent
// TodoActivityScreen(todoViewModel)
TodoItemInput(
Log.e(TAG, "onCreate: $it.task")
)
效果
以上是关于Kotlin jetpack compose 文本输入框ExitText/TextField remember 居然可以传两个参数的主要内容,如果未能解决你的问题,请参考以下文章
Android Kotlin Jetpack Compose 使用
Kotlin 1.5.0 上的 Jetpack Compose
Kotlin Jetpack Compose remember 给Icon添加透明度
Jetpack Compose开源:基于Kotlin的响应式编程方案,简化UI开发