gpt4 book ai didi

android - 单选按钮和单选组

转载 作者:行者123 更新时间:2023-12-05 00:09:59 25 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,我想编写一个有 4 个单选按钮的 radioGroup。我的目的是检查这些单选按钮是否被选中(知道我一次只能选择一个)并将选中的单选按钮的信息保存在一个变量中。下面是kotlin中的代码和xml中的代码。由于 if 的条件,我无法运行。我正在尝试使用 isChecked 来验证单选按钮是否被选中。

    fun button_ConfIniciais(view: View) {

val encarregado = "Nome Próprio Apelido"
val email = "nomeproprioapelido@hotmail.com"
val palavrachave = "123"
val checkedId = radioGroup_Ano.checkedRadioButtonId

if (Text_Mail.text.isEmpty() &&
Text_Pass.text.isEmpty() &&
Text_Enc.text.isEmpty() &&
Text_ConfPass.text.isEmpty()){

val builder = AlertDialog.Builder(this@Configuracoes_Iniciais)

builder.setTitle("Atenção")
builder.setMessage("Deve preencher todos os campos.")
builder.setPositiveButton("Continuar") { dialog, which ->
}

val dialog: AlertDialog = builder.create()
dialog.show()
} else {

//I want to see if some of the radio button is selected, so I use the isChecked
if (Text_Mail.text.trim().toString().equals(email) &&
Text_Pass.text.trim().toString().equals(palavrachave) &&
Text_Enc.text.trim().toString().equals(encarregado) &&
Text_ConfPass.text.trim().toString().equals(palavrachave) &&
(radioGroup_Ano.radioButton_1.isChecked ||
radioGroup_Ano.radioButton_2.isChecked ||
radioGroup_Ano.radioButton_3.isChecked ||
radioButton_4.isChecked)) {

val builder = AlertDialog.Builder(this@Configuracoes_Iniciais)

builder.setTitle("Configurações Iniciais")
builder.setMessage("Bem-Vindo ao Aprende Comigo! Agora que já configurou os seus dados está pronto para aprender!")
builder.setPositiveButton("Seguinte") { dialog, which ->
val it = Intent(this, Bem_Vindo_1::class.java)
startActivity(it)
}

val dialog: AlertDialog = builder.create()
dialog.show()

} else {
val builder = AlertDialog.Builder(this@Configuracoes_Iniciais)

builder.setTitle("Atenção")
builder.setMessage("Palavras-chaves não coicidem.")
builder.setPositiveButton("Continuar") { dialog, which ->
}

val dialog: AlertDialog = builder.create()
dialog.show()
}

}
}

最佳答案

在 Java 中:int id = radioGroup_Ano.getCheckedRadioButtonId();
在 Kotlin 中:val id = radioGroup_Ano.checkedRadioButtonId
这就是检查单选按钮 ID 的方法。
如果你想为选中的单选按钮做一些特定的事情,做这样的事情:

when (id) {
R.id.radioButton_1 -> your code here
R.id.radioButton_2 -> your code here
R.id.radioButton_3 -> your code here
R.id.radioButton_4 -> your code here
else -> your code here
}

关于android - 单选按钮和单选组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51196434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com