代码之家  ›  专栏  ›  技术社区  ›  Danilo Volles

将TextFieldValue返回转换为Double(Kotlin,Jetpack Compose)

  •  0
  • Danilo Volles  · 技术社区  · 3 年前

    那里

    我正在用Jetpack Compose编写一个BMI计算器。 我设置了两个“TextFieldValue”函数来输入用户的身高和体重。两个函数都返回“text”,我无法将其转换为Double类型,因此我可以进行数学运算!

    我已经坚持了三个多小时了! 我刚开始从事安卓开发,所以非常感谢您的帮助! 好吧,非常感谢!

    我尝试过:

    // Insert function:
    
    fun insertWeight (): TextFieldValue {
        var text by remember {
            mutableStateOf(TextFieldValue(""))
        }
        TextField(
            value = text,
            onValueChange = {
                text = it
            },
            label = {
                Text (text = "Weight")
            },
            placeholder = {
                Text (text = "Insert your weight in Kilograms")
            },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
        )
        return text
    }
    

    然后(在主活动中)

    // I couldn't find out how to do it directly without passing by String (I even don't know if it is right"
    val weight: Double = insertWeight().toString().toDouble()
    

    我还尝试了另一种方法,将函数类型从“TextFieldValue”更改为“Double”,然后将函数中的return更改为“return text.toString().toDouble()”,然后我意识到尝试这是一件非常愚蠢的事情哈哈哈哈如果类型不匹配,compose函数将致命地终止应用程序!

    0 回复  |  直到 3 年前
        1
  •  0
  •   Jayant Kumar    3 年前

    您不能使用 composable function 在正常函数中,或者不能返回数据(因为可组合函数不是正常函数,它是一个小部件或Ui),您可以从下面的代码中实现上述内容。希望它能帮助你。

    @Composable
    fun TextFieldValueScreen() {
    
        var weight by remember { mutableStateOf("") }
        var height by remember { mutableStateOf("") }
        var total by remember { mutableStateOf(0.0) }
        val context = LocalContext.current
    
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(20.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            CommonTextField(
                title = "Weight",
                placeholder = "Insert your weight in Kilograms",
                text = weight,
                onValueChange = { weight = it }
            )
            Spacer(modifier = Modifier.height(20.dp))
            CommonTextField(
                title = "Height",
                placeholder = "Insert your height in meter",
                text = height,
                onValueChange = { height = it }
            )
            Spacer(modifier = Modifier.height(20.dp))
            Button(onClick = {
                if (height.isNotEmpty() && weight.isNotEmpty()) {
                    total = (weight.toDouble()) / (height.toDouble() * height.toDouble())
                } else {
                    Toast.makeText(context, "Please type height and weight", Toast.LENGTH_SHORT).show()
                }
            }) {
                Text(text = "Calculate")
            }
            Spacer(modifier = Modifier.height(20.dp))
            if (total != 0.0)
                Text(
                    text = "BMI is ${String.format(Locale.US, "%.2f", total)}",
                    style = TextStyle(
                        fontSize = 25.sp,
                        fontWeight = FontWeight.SemiBold,
                        fontStyle = FontStyle.Italic,
                    )
                )
        }
    
    }
    
    @Composable
    fun CommonTextField(
        title: String,
        placeholder: String,
        text: String,
        onValueChange: (String) -> Unit
    ) {
    
        TextField(
            value = text,
            onValueChange = onValueChange,
            label = {
                Text(text = title)
            },
            placeholder = {
                Text(text = placeholder)
            },
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Phone)
        )
    
    }
    

    输出

    enter image description here