如何在Android Jetpack Compose中使用Kotlin限制参数的类型?

huangapple go评论62阅读模式
英文:

How can I restrict the type of a parameter in Android Jetpack Compose with Kotlin?

问题

Code A

@Composable
fun DisplayEditBox(
    editTextContent: String,
    onChangEditTextContent: (String) -> Unit
) {
    TextField(
        value = editTextContent,
        onValueChange = onChangEditTextContent
    )
}

@Composable
fun ScreenHome_Line() {
    var editContent by remember { mutableStateOf("Hello") }
    DisplayEditBox(editContent) {
        editContent = it
    }
}

Code B

@Composable
fun DisplayEditBox(
    editTextContent: String = remember { mutableStateOf("") },
    onChangEditTextContent: (String) -> Unit
) {
    TextField(
        value = editTextContent,
        onValueChange = onChangEditTextContent
    )
}

Code C

@Composable
fun DisplayEditBox(
    editTextContent: String by remember { mutableStateOf("") },
    onChangEditTextContent: (String) -> Unit
) {
    TextField(
        value = editTextContent,
        onValueChange = onChangEditTextContent
    )
}
英文:

I can use Code A to display a dialog of edit box. In fact, the parameter editTextConten need to accept a variable just like var editContent by remember { mutableStateOf("Hello") } .

How can I restrict a type of parameter ?

BTW, the Code B and Code C are wrong.

Code A

@Composable
fun DisplayEditBox(
    editTextContent:String,
    onChangEditTextContent: (String) ->Unit
) {
    TextField(
        value = editTextContent,
        onValueChange = onChangEditTextContent
    )
}

@Composable
fun ScreenHome_Line() {
    var editContent by remember { mutableStateOf("Hello") }
    DisplayEditBox(editContent) {
        editContent = it
    }
}

Code B

@Composable
fun DisplayEditBox(
    editTextContent:String = remember {mutableStateOf("") },
    onChangEditTextContent: (String) ->Unit
) {
    TextField(
        value = editTextContent,
        onValueChange = onChangEditTextContent
    )
}

Code C

@Composable
fun DisplayEditBox(
    editTextContent:String by remember {mutableStateOf("") },
    onChangEditTextContent: (String) ->Unit
) {
    TextField(
        value = editTextContent,
        onValueChange = onChangEditTextContent
    )
}

答案1

得分: 1

你可以使用 State<*> 作为类型。

@Composable
fun DisplayEditBox(
editTextContent: State<String>,
onChangEditTextContent: (String) -> Unit
) {
TextField(
value = editTextContent.value,
onValueChange = onChangEditTextContent
)
}

@Composable
fun ScreenHome_Line() {
val editContent = remember { mutableStateOf("Hello") }
DisplayEditBox(editContent) {
editContent.value = it
}
}

现在你可以只发送 State 值作为参数,它将强制调用站点处理你的代码的状态,并防止调用站点发送原始字符串作为参数。

英文:

You can use State<*> as type.

@Composable
fun DisplayEditBox(
    editTextContent: State&lt;String&gt;,
    onChangEditTextContent: (String) -&gt; Unit
) {
    TextField(
        value = editTextContent.value,
        onValueChange = onChangEditTextContent
    )
}

@Composable
fun ScreenHome_Line() {
    val editContent = remember { mutableStateOf(&quot;Hello&quot;) }
    DisplayEditBox(editContent) {
        editContent.value = it
    }
}

Now you can send only State value as parameter and it will force call site to handle state for your code. And prevent call site to send raw string as parameter.

答案2

得分: 0

很遗憾,您不能这样做,但您可以进行state hoisting,这是建议您在第一个示例中要做的。

英文:

Unfortunately you can't do that, but you can do the state hoisting, which is what you're advised to do as in the first example.

huangapple
  • 本文由 发表于 2023年6月1日 15:08:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定