Anonymous
Нажатие кнопки - это переменные инициализации при изменении выбора RadioButton
Сообщение
Anonymous » 30 апр 2025, 12:05
I'm working on a very simple app that asks some questions which are stored in a db with their respective answer choices, I'm using radio buttons to display the answer choices to the users, but when a user changes their answer in the radio buttons the variables that are used to get the next question and aswer set gets reinitialized back to one for some reason, even though so far clicking on the radio buttons does't do anything other than just change the selection, anyone ran into a similar issue? Или может указать, что, черт возьми, я делаю не так/не вижу?
Код: Выделить всё
package com.example.tesis.vista
import android.content.Context
import android.util.Log
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.absoluteOffset
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.selection.selectableGroup
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Divider
import androidx.compose.material.RadioButton
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.PaintingStyle
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.ImageLoader
import coil.compose.AsyncImage
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import com.example.tesis.R
import com.example.tesis.logica.DbClass
import kotlin.math.cos
import kotlin.math.sin
@Composable
fun TestScreen(navigationViewModel: NavigationViewModel, context: Context) {
// Importar fuente para letras
val mogra = FontFamily(
Font(R.font.mogra_regular, FontWeight.Light),
Font(R.font.mogra_regular, FontWeight.Normal),
Font(R.font.mogra_regular, FontWeight.Normal, FontStyle.Italic),
Font(R.font.mogra_regular, FontWeight.Medium),
Font(R.font.mogra_regular, FontWeight.Bold)
)
val db = remember { DbClass.getDatabase(context) }
val preguntaDao = db.preguntaDao()
val respuestaDao = db.respuestaDao()
var idPregunta = 1
var idRespuesta = 1
val pregunta = remember { mutableStateOf(preguntaDao.getTextoPreguntaById(idPregunta))}
val cantidadPreguntas = preguntaDao.getCantidadPreguntas()
val respuesta = remember {mutableStateOf(respuestaDao.getRespuestaById(idRespuesta))}
//Contador de pregunta actual para rastrear progreso
val currentStep = remember { mutableStateOf(0) }
//val listaResp: List = respuesta.value.split(",")
val radioOptions: List = (respuesta.value.split(","))
val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[0]) }
//FocusManager para cerrar el teclado en pantalla al tocar fuera
var localFocusManager = LocalFocusManager.current
Surface (modifier = Modifier
.fillMaxSize(),
color = Color(0xFFFFFF99)
){
//Columna contenedora
Column() {
//Fila superior para mapa y Soly
Row(
modifier = Modifier.fillMaxWidth().weight(1f),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.Top
){
//Columna mapa
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.weight(1f)
) {
StepsProgressBar(modifier = Modifier.fillMaxWidth(), numberOfSteps = cantidadPreguntas, currentStep = currentStep.value)
}
//Columna Soly
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.weight(1f)
) {
//GifPreview()
Image(
painter = painterResource(id = R.drawable.monstruo_2),
contentDescription = "Soly",
modifier = Modifier
.size(300.dp)
)
}
}
//Fila enunciado pregunta
Row(
modifier = Modifier.weight(1f)
) {
Text(
text = pregunta.value,
fontSize = 50.sp,
fontWeight = FontWeight.Bold,
color = Color(0xFF34D9A2),
fontFamily = mogra,
modifier = Modifier
.wrapContentSize(Alignment.Center)
.padding(12.dp)
)
}
//Fila respuestas pregunta
Row(
modifier = Modifier.weight(1f).selectableGroup()
) {
radioOptions.forEach {
text->
Column(
modifier = Modifier
.fillMaxWidth(0.5f)
.selectable(selected = (text == selectedOption),
onClick = { onOptionSelected(text) },
role = Role.RadioButton
),
horizontalAlignment = Alignment.CenterHorizontally
) {
RadioButton(
selected = (text==selectedOption),
onClick = null
)
Text(
text = text
)
}
}
}
}
Row (modifier = Modifier.absoluteOffset(0.dp, 50.dp)){
Spacer(modifier = Modifier.width(300.dp))
Column (modifier = Modifier.align(Alignment.CenterVertically)){
Spacer(modifier = Modifier.height(10.dp))
Spacer(modifier = Modifier.height(20.dp))
Button(
onClick = {
if (currentStep.value
Подробнее здесь: [url]https://stackoverflow.com/questions/79597137/button-press-is-re-initializing-variables-when-changing-radiobutton-selection[/url]
1746003931
Anonymous
I'm working on a very simple app that asks some questions which are stored in a db with their respective answer choices, I'm using radio buttons to display the answer choices to the users, but when a user changes their answer in the radio buttons the variables that are used to get the next question and aswer set gets reinitialized back to one for some reason, even though so far clicking on the radio buttons does't do anything other than just change the selection, anyone ran into a similar issue? Или может указать, что, черт возьми, я делаю не так/не вижу?[code]package com.example.tesis.vista import android.content.Context import android.util.Log import androidx.compose.foundation.Canvas import androidx.compose.foundation.Image import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.absoluteOffset import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.wrapContentSize import androidx.compose.foundation.layout.wrapContentWidth import androidx.compose.foundation.selection.selectable import androidx.compose.foundation.selection.selectableGroup import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.Button import androidx.compose.material.ButtonDefaults import androidx.compose.material.Divider import androidx.compose.material.RadioButton import androidx.compose.material.Surface import androidx.compose.material.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Paint import androidx.compose.ui.graphics.PaintingStyle import androidx.compose.ui.graphics.Path import androidx.compose.ui.graphics.PathEffect import androidx.compose.ui.graphics.drawscope.drawIntoCanvas import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.res.painterResource import androidx.compose.ui.semantics.Role import androidx.compose.ui.text.font.Font import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import coil.ImageLoader import coil.compose.AsyncImage import coil.decode.GifDecoder import coil.decode.ImageDecoderDecoder import com.example.tesis.R import com.example.tesis.logica.DbClass import kotlin.math.cos import kotlin.math.sin @Composable fun TestScreen(navigationViewModel: NavigationViewModel, context: Context) { // Importar fuente para letras val mogra = FontFamily( Font(R.font.mogra_regular, FontWeight.Light), Font(R.font.mogra_regular, FontWeight.Normal), Font(R.font.mogra_regular, FontWeight.Normal, FontStyle.Italic), Font(R.font.mogra_regular, FontWeight.Medium), Font(R.font.mogra_regular, FontWeight.Bold) ) val db = remember { DbClass.getDatabase(context) } val preguntaDao = db.preguntaDao() val respuestaDao = db.respuestaDao() var idPregunta = 1 var idRespuesta = 1 val pregunta = remember { mutableStateOf(preguntaDao.getTextoPreguntaById(idPregunta))} val cantidadPreguntas = preguntaDao.getCantidadPreguntas() val respuesta = remember {mutableStateOf(respuestaDao.getRespuestaById(idRespuesta))} //Contador de pregunta actual para rastrear progreso val currentStep = remember { mutableStateOf(0) } //val listaResp: List = respuesta.value.split(",") val radioOptions: List = (respuesta.value.split(",")) val (selectedOption, onOptionSelected) = remember { mutableStateOf(radioOptions[0]) } //FocusManager para cerrar el teclado en pantalla al tocar fuera var localFocusManager = LocalFocusManager.current Surface (modifier = Modifier .fillMaxSize(), color = Color(0xFFFFFF99) ){ //Columna contenedora Column() { //Fila superior para mapa y Soly Row( modifier = Modifier.fillMaxWidth().weight(1f), horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.Top ){ //Columna mapa Column( verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.weight(1f) ) { StepsProgressBar(modifier = Modifier.fillMaxWidth(), numberOfSteps = cantidadPreguntas, currentStep = currentStep.value) } //Columna Soly Column( verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.weight(1f) ) { //GifPreview() Image( painter = painterResource(id = R.drawable.monstruo_2), contentDescription = "Soly", modifier = Modifier .size(300.dp) ) } } //Fila enunciado pregunta Row( modifier = Modifier.weight(1f) ) { Text( text = pregunta.value, fontSize = 50.sp, fontWeight = FontWeight.Bold, color = Color(0xFF34D9A2), fontFamily = mogra, modifier = Modifier .wrapContentSize(Alignment.Center) .padding(12.dp) ) } //Fila respuestas pregunta Row( modifier = Modifier.weight(1f).selectableGroup() ) { radioOptions.forEach { text-> Column( modifier = Modifier .fillMaxWidth(0.5f) .selectable(selected = (text == selectedOption), onClick = { onOptionSelected(text) }, role = Role.RadioButton ), horizontalAlignment = Alignment.CenterHorizontally ) { RadioButton( selected = (text==selectedOption), onClick = null ) Text( text = text ) } } } } Row (modifier = Modifier.absoluteOffset(0.dp, 50.dp)){ Spacer(modifier = Modifier.width(300.dp)) Column (modifier = Modifier.align(Alignment.CenterVertically)){ Spacer(modifier = Modifier.height(10.dp)) Spacer(modifier = Modifier.height(20.dp)) Button( onClick = { if (currentStep.value Подробнее здесь: [url]https://stackoverflow.com/questions/79597137/button-press-is-re-initializing-variables-when-changing-radiobutton-selection[/url]