如何创建动态文本字段并从 Jetpack Compose 中的每个文本字段获取值?

how create dynamic textfield and get value from each textfield in jetpack compose?

假设我有这个可组合函数来创建 outlineTextField

val valueState = remember { mutableStateOf("") }

myLists.forEach{ item -> 
             OutlinedTextField(
                value = valueState.value,
                onValueChange = {
                    valueState.value = it
                }
             )
    
    }

Button(
    onClick= { 
      valueState.value  // want to print each value from OutlinedTextField here..
    }
){
     Text("Click Me")
}

OutlinedTextField 的数量将基于循环创建。

问题

如何从每个文本字段获取值?

我想你可以使用 mutableStateListOf

val myLists = listOf("xx","xx","xxx")
val textFieldInitValues = List(myLists.size){ "" }
val valueStateList = remember { mutableStateListOf<String>().apply { addAll(textFieldInitValues) } }

Column {
    myLists.forEachIndexed { index, item ->
        OutlinedTextField(
            value = valueStateList[index],
            onValueChange = {
                valueStateList[index] = it
            }
        )

    }

    Button(
        onClick = {
            valueStateList.forEach {
                Log.d("Button", it)
            }
        }
    ) {
        Text("Click Me")
    }
}