如何在 Jetpack compose 中使用 EditText 或 TextInput 小部件?

问题描述 投票:0回答:6

我通过尝试一些像 Image 和 EditText 这样的小部件来探索 Jetpack compose。

对于文本输入,它有

EditableText
。我试过下面的代码,但它没有在 UI 中显示任何内容

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            loadUi()
        }
    }

    @Composable
    fun loadUi() {
        CraneWrapper {
            MaterialTheme {
                val state = +state { EditorState("") }
                EditableText(
                    value = state.value,
                    onValueChange = { state.value = it },
                    editorStyle = EditorStyle(
                        textStyle = TextStyle(
                            fontSize = (50f)
                        )
                    )
                )
            }
        }
    }
}

我在这里缺少什么?任何帮助将不胜感激!

android androidx android-jetpack-compose android-compose-textfield
6个回答
46
投票

Gabriele Mariotti 的回答所述,这是正确的做法:

var text by rememberSaveable { mutableStateOf("Text") }

TextField(
    value = text,
    onValueChange = {
        text = it
    },
    label = { Text("Label") }
)

但是,如果您遇到以下错误:

Type 'TypeVariable(T)' 没有方法 'getValue(MainActivity, KProperty<*>)' 因此它不能作为委托

只需将这两个导入添加到您的文件中就可以了:

import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue

24
投票

您可以使用

TextField
.

类似的东西:

var text by rememberSaveable { mutableStateOf("Text") }

TextField(
    value = text,
    onValueChange = {
        text = it
    },
    label = { Text("Label") }
)

其他细节:

https://developer.android.com/jetpack/compose/text#enter-modify-text


3
投票

抱歉回答晚了。 API 发生了一些变化,所以您的代码现在应该如下所示:

@Composable
fun loadUi() {
    val state = +state { EditorModel("smth") }
    TextField(
        value = state.value,
        onValueChange = { state.value = it },
        editorStyle = EditorStyle(
            textStyle = TextStyle(
                fontSize = (50.sp)
            )
        )
    )
}

你也可能会错过小部件,因为它没有默认背景,如果你有空字符串,默认情况下它几乎是不可见的


2
投票
TextField(
    value = state.value,
    onValueChange = { new ->
        state.value = if (new.text.any { it == '\n' }) {
            state.value
        } else {
            new
        }
    },
    keyboardType = KeyboardType.Text,
    imeAction = ImeAction.Search,
    textStyle = TextStyle(color = Color.DarkGray),
    onImeActionPerformed = onImeActionPerformed
)

0
投票

下面是一些可组合的文本字段。请选择您要查找的内容:

@Composable
fun SimpleFilledTextFieldSample() {
    var text by remember { mutableStateOf("Hello") }
    TextField(
        value = text,
        onValueChange = { text = it },
        label = { Text("Label") }
    )
}


@Composable
fun SimpleOutlinedTextFieldSample() {
    var text by remember { mutableStateOf("") }
    OutlinedTextField(
        value = text,
        onValueChange = { text = it },
        label = { Text("Label") }
    )
}


@Composable
fun StyledTextField() {
    var value by remember { mutableStateOf("Hello\nWorld\nInvisible") }

    TextField(
        value = value,
        onValueChange = { value = it },
        label = { Text("Enter text") },
        maxLines = 2,
        textStyle = TextStyle(color = Color.Blue, fontWeight = FontWeight.Bold),
        modifier = Modifier.padding(20.dp)
    )
}


@Composable
fun PasswordTextField() {
    var password by rememberSaveable { mutableStateOf("") }
    TextField(
        value = password,
        onValueChange = { password = it },
        label = { Text("Enter password") },
        visualTransformation = PasswordVisualTransformation(),
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password)
    )
}

阅读更多:

撰写文本 >> https://developer.android.com/jetpack/compose/text

视频 >> https://www.youtube.com/watch?v=_qls2CEAbxI&ab_channel=AndroidDevelopers

输出:

祝你有美好的一天!


-1
投票
 val state = +state {
                    EditorModel("Edit Text")
                }
                TextField(
                    value = state.value,
                    onValueChange = {
                        state.value = it
                    },
                    textStyle = TextStyle(
                        fontSize = (30.sp),
                        color = Color.DarkGray
                    ),
                    keyboardType = KeyboardType.Text
                )

试试这个。

© www.soinside.com 2019 - 2024. All rights reserved.