RadioButton как вычислить значение

При вводе аргумента x и нажатии Button проверяется в какой из трех диапазонов попадает x, выполняются вычисления и результат отображается в TextView. При этом соответствующая радиокнопка становится отмеченной. Если далее нажать другую радиокнопку, вычисления пересчитываются для формулы, соответствующей этой кнопки, в TextView выводится новый результат, диапазон, в котором находится x игнорируется. Проблема в том, что при нажатии на другую радиокнопку, она все равно считает по формуле, в которое попадает значение X. Подскажите пожалуйста, как сделать

Код программы

MainActivity.java

package com.example.test
      import android.os.Bundle
      import android.widget.Button
      import android.widget.Button
      import android.widget.CheckBox
      import android.widget.EditText
      import android.widget.RadioButton
      import android.widget.TextView
      import androidx.appcompat.app.AppCompatActivity
      import kotlin.math.pow
      import kotlin.math.sin
      import kotlin.math.sqrt
  class MainActivity : AppCompatActivity() {
private lateinit var editTextX: EditText
private lateinit var buttonCalculate: Button
private lateinit var textViewResult: TextView
private lateinit var radioButtonFormula1: RadioButton
private lateinit var radioButtonFormula2: RadioButton
private lateinit var radioButtonFormula3: RadioButton
private lateinit var checkBoxDouble: CheckBox

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    editTextX = findViewById(R.id.editTextX)
    buttonCalculate = findViewById(R.id.buttonCalculate)
    textViewResult = findViewById(R.id.textViewResult)
    radioButtonFormula1 = findViewById(R.id.radioButtonFormula1)
    radioButtonFormula2 = findViewById(R.id.radioButtonFormula2)
    radioButtonFormula3 = findViewById(R.id.radioButtonFormula3)
    checkBoxDouble = findViewById(R.id.checkBoxDouble)

    buttonCalculate.setOnClickListener {
        calculateResult()
    }

    // Начальная установка радиокнопок
    radioButtonFormula1.isChecked = false
}

private fun calculateResult() {
    val x = editTextX.text.toString().toDoubleOrNull()

    if (x != null) {
        val result = when {
            x <= 2 -> {
                radioButtonFormula1.isChecked = true
                sin(x + 1)
            }
            x > 2 && x < 3 -> {
                radioButtonFormula2.isChecked = true
                sqrt(x.pow(3) - 1)
            }
            else -> {
                radioButtonFormula3.isChecked = true
                x + 1.5
            }
        }

        if (checkBoxDouble.isChecked) {
            textViewResult.text = (result * 2).toString()
        } else {
            textViewResult.text = result.toString()
        }
    } else {
        textViewResult.text = "Введите корректное значение x"
    }
} }

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="100dp">

<Button
    android:id="@+id/buttonCalculate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/checkBoxDouble"
    android:layout_marginTop="100dp"
    android:text="Вычислить" />


<CheckBox
    android:id="@+id/checkBoxDouble"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="220dp"
    android:text="Удвоить результат" />

<EditText
    android:id="@+id/editTextX"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="0dp"
    android:hint="Введите значение x"
    android:inputType="numberDecimal" />

<TextView
    android:id="@+id/textViewResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/buttonCalculate"
    android:layout_marginTop="16dp"
    android:text=""
    android:textSize="18sp" />

<RadioGroup
    android:id="@+id/radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp">


    <RadioButton
        android:id="@+id/radioButtonFormula1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Формула 1" />

    <RadioButton
        android:id="@+id/radioButtonFormula2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Формула 2" />

    <RadioButton
        android:id="@+id/radioButtonFormula3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Формула 3" />
</RadioGroup> </RelativeLayout>

Ответы (0 шт):