2

我想在每次用户单击“添加”按钮时将文本字段动态添加到我的布局中。添加的文本字段应添加到“添加”按钮上方。即在步骤 1 文本字段和添加按钮之间。如何通过 Jetpack Compose 实现?下面是屏幕截图其次是我当前的代码..

在此处输入图像描述

代码-

Column(modifier = Modifier.padding(16.dp)) {

            OutlinedTextField(
                modifier = Modifier.fillMaxWidth(),
                value = step1,
                onValueChange = {
                    viewModel.onStep1Changed(it)
                },
                label = {
                    Text(text = "Step 1...")
                },
                shape = RoundedCornerShape(8.dp),
                colors = TextFieldDefaults.textFieldColors(
                    backgroundColor = Color.Transparent),
                trailingIcon = {
                    Icon(
                        modifier = Modifier.padding(start=10.dp),
                        imageVector = Icons.Filled.Image,
                        tint= Color.Blue,
                        contentDescription = "Select Image"
                    )
                }
            )
            Spacer(modifier = Modifier.height(16.dp))
            Button(onClick = {
             //TODO Dynamically add text fields
            }){
                Text("Add")
            }

        }
4

1 回答 1

2
Column(modifier = Modifier.padding(16.dp)) {
val textFieldCount by remember { mutableStateOf (1) }
            repeat(textFieldCount) {
                OutlinedTextField(
                modifier = Modifier.fillMaxWidth(),
                value = step1,
                onValueChange = {
                    viewModel.onStep1Changed(it)
                },
                label = {
                    Text(text = "Step 1...")
                },
                shape = RoundedCornerShape(8.dp),
                colors = TextFieldDefaults.textFieldColors(
                    backgroundColor = Color.Transparent),
                trailingIcon = {
                    Icon(
                        modifier = Modifier.padding(start=10.dp),
                        imageVector = Icons.Filled.Image,
                        tint= Color.Blue,
                        contentDescription = "Select Image"
                    )
                }
            )
            Spacer(modifier = Modifier.height(16.dp))
        }
            Button(onClick = {
             textFieldCount++
            }){
                Text("Add")
            }

        }
于 2021-07-13T09:33:02.060 回答