英文:
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<String>,
onChangEditTextContent: (String) -> Unit
) {
TextField(
value = editTextContent.value,
onValueChange = onChangEditTextContent
)
}
@Composable
fun ScreenHome_Line() {
val editContent = remember { mutableStateOf("Hello") }
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论