下拉菜单重置值并将其设为空字符串

问题描述 投票:0回答:1

我想在选择新位置时重置该值并将其设置为空字符串

我尝试像这样重置值:

**tower.value = ""

floorNumber.value = ""**

但在 UI 方面,价值仍然存在

**这是下拉菜单 **

    @Composable
    fun DropDownBox(
        items:List<String>,
        onItemSelected: (String) -> Unit,
        textFieldLabel:String,
        modifier: Modifier = Modifier,
    ) {
        var expanded by remember { mutableStateOf(false) }
        var selectedText by remember { mutableStateOf("") }

        var textfieldSize by remember { mutableStateOf(Size.Zero) }

        val icon = if (expanded)
            Icons.Filled.ArrowDropUp //it requires androidx.compose.material:material-icons-extended
        else
            Icons.Filled.ArrowDropDown


        Log.d("select", "$selectedText")
        Column() {
            OutlinedTextField(
                textStyle = TextStyle(
                    fontFamily = mont,
                    color = Color.Black),
                shape = RoundedCornerShape(15.dp),
                value = selectedText,
                onValueChange = {
                    selectedText = it },
                modifier = Modifier
                    .fillMaxWidth()
                    .onGloballyPositioned { coordinates ->
                        //This value is used to assign to the DropDown the same width
                        textfieldSize = coordinates.size.toSize()
                    },
                label ={ Text(textFieldLabel,
                    fontSize = 15.sp,
                    fontWeight = FontWeight.Normal,
                    fontFamily = mont
                )
                },
                trailingIcon = {
                    Icon(icon,"contentDescription",
                        Modifier.clickable { expanded = !expanded })
                },
                readOnly = true,
            )
            DropdownMenu(
                expanded = expanded,
                onDismissRequest = { expanded = false },
                modifier = Modifier
                    .width(with(LocalDensity.current){textfieldSize.width.toDp()})
            ) {
                items.sorted().forEach { label ->
                    DropdownMenuItem(onClick = {
                        selectedText = label
                        onItemSelected(selectedText)
                        expanded =  false
                    }) {
                        Text(text = label,
                            fontSize = 15.sp,
                            fontWeight = FontWeight.Normal,
                            fontFamily = mont
                        )
                    }
                }
            }
        }
     }

我有一个下拉菜单,可显示位置、塔楼和楼层号。我希望如果我选择一个新位置,塔楼和楼层号的值将重置为空字符串。我已经尝试过这段代码,但唯一的变化是值而不是 UI 本身。所以我正在尝试调试这段代码,请帮助我解决我的代码

                            var isLocationSelected by remember { mutableStateOf(false) }

                            var isTowerSelected by remember { mutableStateOf(false) }

                            var isFloorSelected by remember { mutableStateOf(false) }

                            Log.d("LOCATION", "$location")
                            DropdownTextField(
                                items = locationsMap.map {
                                    it.key
                                },
                                onItemSelected = {
                                    if (locationErrorState.value) {
                                        locationErrorState.value = false
                                    }
                                    location.value = it

                                    isLocationSelected = true

                                    isTowerSelected = false
                                    isFloorSelected = false

                                    tower.value = ""
                                    floorNumber.value = ""
                                    unitNumber.value = ""
                                    unitNumberSuffx.value = ""

                                    /* Populate towers dropdown list */
                                    var towersList:MutableList<String> = ArrayList()
                                    towers.value = towersList

                                    var suffixList:MutableList<String> = ArrayList()
                                    suffix.value = suffixList

                                    val building = locationsMap[location.value]
                                    building?.let {
                                        it.map {
                                            it.towerName?.let {
                                                towersList.add(it)
                                            }

                                            it.unitNumberSuffix.let {
                                                it?.let {
                                                    it.map {
                                                        suffixList.add(it)
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    if (!towersList.isEmpty()) {
                                        towers.value = towersList
                                    } else {
                                        building?.let {
                                            val maxFloorNumber = it.first().floorNumber
                                            val maxUnit = it.first().unitsPerFloor

                                            var unitList: MutableList<String> = ArrayList()
                                            var floorsList: MutableList<String> = ArrayList()

                                            for (n in 1..maxFloorNumber.toInt()) {
                                                floorsList.add(n.toString())
                                            }

                                            for (n in 1..maxUnit) {
                                                // Add "0" in the middle for unit numbers 1-9
                                                val formattedUnit = if (n in 1..9) "0$n" else n.toString()
                                                unitList.add(formattedUnit)
                                            }

                                            unitNumbers.value = unitList
                                            floors.value = floorsList
                                        }
                                    }


                                    if (!suffixList.isEmpty()) {
                                        suffix.value = suffixList.distinct()
                                    }



                                },
                                textFieldLabel = "Location",
                            )


                            Log.d("TOWER", "$tower")
                            if (isLocationSelected && towers.value.count() >= 1) {
                                Spacer(modifier = Modifier.height(8.dp))
                                DropdownTextField(items = towers.value,
                                    onItemSelected = {
                                        if (towerErrorState.value) {
                                            towerErrorState.value = true
                                        }
                                        tower.value = it

                                        isTowerSelected = true

                                        isFloorSelected = false
                                        floorNumber.value = ""

                                        /*
                                        Update floors
                                         */
                                        val building = locationsMap[location.value]
                                        building?.let {
                                            it.map {
                                                if (it.towerName == tower.value) {
                                                    val maxFloorNumber = it.floorNumber
                                                    val maxUnit = it.unitsPerFloor

                                                    var unitList:MutableList<String> = ArrayList()
                                                    var floorsList:MutableList<String> = ArrayList()
                                                    for(n in 1..maxFloorNumber.toInt()) {
                                                        floorsList.add(n.toString())
                                                    }
                                                    for (n in 1..maxUnit) {
                                                        // Add "0" in the middle for unit numbers 1-9
                                                        val formattedUnit = if (n in 1..9) "0$n" else n.toString()
                                                        unitList.add(formattedUnit)
                                                    }
                                                    unitNumbers.value = unitList
                                                    floors.value = floorsList
                                                }
                                            }

                                        }
                                    },
                                    textFieldLabel = "Tower",
                                )
                            }

                            Spacer(modifier = Modifier.height(8.dp))



                            Log.d("FLOOR", "$floorNumber")
                            if (isLocationSelected || isTowerSelected) {
                                DropDownBox(
                                    items = floors.value,
                                    onItemSelected = {
                                        if (floorNumberErrorState.value) {
                                            floorNumberErrorState.value = false
                                        }
                                        Log.d("TEST", "$it")
                                        floorNumber.value = it
                                        isFloorSelected = true

                                        unitNumber.value = ""


                                    },
                                    textFieldLabel = stringResource(R.string.FloorNumber),

                                )
                            }
android kotlin mobile android-jetpack-compose
1个回答
0
投票

为了确保在选择新位置时重置 DropDownBox 值,您可以添加逻辑以重置 DropDownBox 可组合项本身中的选定值。下面是 DropDownBox 可组合项的修改版本,添加了在选择新位置时重置所选文本的逻辑:

@Composable
fun DropDownBox(
    items: List<String>,
    onItemSelected: (String) -> Unit,
    textFieldLabel: String,
    modifier: Modifier = Modifier,
) {
    var expanded by remember { mutableStateOf(false) }
    var selectedText by remember { mutableStateOf("") }

    var textfieldSize by remember { mutableStateOf(Size.Zero) }

    val icon = if (expanded)
        Icons.Filled.ArrowDropUp
    else
        Icons.Filled.ArrowDropDown

    Column {
        OutlinedTextField(
            value = selectedText,
            onValueChange = {
                selectedText = it
            },
            modifier = Modifier
                .fillMaxWidth()
                .onGloballyPositioned { coordinates ->
                    textfieldSize = coordinates.size.toSize()
                },
            label = {
                Text(textFieldLabel)
            },
            trailingIcon = {
                Icon(icon, contentDescription = "Dropdown Icon",
                    modifier = Modifier.clickable { expanded = !expanded })
            },
            readOnly = true,
        )
        DropdownMenu(
            expanded = expanded,
            onDismissRequest = { expanded = false },
            modifier = Modifier.width(with(LocalDensity.current) {
                textfieldSize.width.toDp()
            })
        ) {
            items.sorted().forEach { label ->
                DropdownMenuItem(onClick = {
                    selectedText = label
                    onItemSelected(selectedText)
                    expanded = false
                }) {
                    Text(text = label)
                }
            }
        }
    }

    // Reset selected text when a new location is selected
    DisposableEffect(Unit) {
        onDispose {
            selectedText = ""
        }
    }
}

通过此修改,每当选择新位置时,DropDownBox 中的 selectedText 状态变量将重置为空字符串,从而有效地清除 DropDownBox UI 中显示的文本。这可确保选择新位置时 UI 反映更新后的状态。

© www.soinside.com 2019 - 2024. All rights reserved.