android 如何在Jetpack撰写中使用EditText或TextInput小部件?

fcipmucu  于 2023-05-05  发布在  Android
关注(0)|答案(6)|浏览(143)

我通过尝试一些小部件(如Image和EditText)来探索Jetpack撰写。
对于文本输入,它有EditableText。我已经尝试了下面的代码,但它没有显示任何在用户界面

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)
                        )
                    )
                )
            }
        }
    }
}

我错过了什么?任何帮助将不胜感激!

j7dteeu8

j7dteeu81#

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

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

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

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

类型“TypeVariable(T)”没有方法“getValue(MainActivity,KProperty〈*〉)”,因此不能用作委托
简单地将这两个导入添加到您的文件中,您就可以了:

import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
8ulbf1ek

8ulbf1ek2#

您可以使用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

wz3gfoph

wz3gfoph3#

抱歉,回答晚了。API做了一点改动,所以你的代码现在应该是这样的:

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

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

eiee3dmh

eiee3dmh4#

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
)
watbbzwu

watbbzwu5#

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

@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

**输出:**x1c 0d1x

祝您愉快!!

zpqajqem

zpqajqem6#

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
                )

试试这个。

相关问题