将TextFieldValue返回值转换为双精度(Kotlin、Jetpack合成)

xvw2m8pv  于 2023-03-09  发布在  Kotlin
关注(0)|答案(1)|浏览(123)

在那儿!
我正在用Jetpack编写一个BMI计算器。我设置了两个"TextFieldValue"函数来输入用户的身高和体重。这两个函数都返回了"文本",我无法将其转换为Double类型,这样我就可以做数学了!
这是beeing超过3个小时,我被困在它!我开始在Android开发,所以你的帮助将非常感谢!好吧,非常感谢!
我试过:

// 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 text. toString(). toDouble()",然后我意识到这是一件非常愚蠢的事情,尝试哈哈哈哈如果类型不匹配,组合函数将致命地终止应用程序!

mm9b1k5b

mm9b1k5b1#

你不能在普通函数中使用composable function或者不能返回数据(因为可组合函数不是普通函数,它是一个widget或者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)
    )

}

输出

相关问题