否则,如果未选中单选按钮时循环未停止,则下一个活动仍会打开



我对Android Studio中的编码很陌生,并且制作了一个症状检查器,允许用户检查一系列是和否单选按钮来回答问题。问题是,即使错过了一个问题(是和否(单选按钮,也会弹出我生成的"toast"通知,但不会阻止用户到达下一个活动(诊断页面(。

我尝试使用 return; 命令尝试停止循环,但它仍然做同样的事情。如果所有问题都得到回答,这不会成为问题。

可以从以下链接访问该应用程序:github.com/Akhlz001/MHT_Application

下面是显示其中两个循环的活动代码的一部分(这些循环重复 9 次(:

public void opendiagnosis() {
if (radioButton.isChecked())
{
Intent intent = new Intent(this, answers.class);
startActivity(intent);
}
else if (radioButton2.isChecked())
{
Intent intent = new Intent(this, answers.class);
startActivity(intent);
}
else
{
Toast.makeText(getBaseContext(), "Please answer all the questions for an accurate diagnosis",
Toast.LENGTH_LONG).show();
return;
}
if (radioButton3.isChecked())
{
Intent intent = new Intent(this, answers.class);
startActivity(intent);
}
else if (radioButton4.isChecked())
{
Intent intent = new Intent(this, answers.class);
startActivity(intent);
}
else
{
Toast.makeText(getBaseContext(), "Please answer all the questions for an accurate diagnosis",
Toast.LENGTH_LONG).show();
return;
}

深入了解问题所在将非常有帮助,谢谢。

如果对所有RadioButton对都进行了选择,则仅要继续下Activity。因此,您可以按如下方式更改代码:

public void opendiagnosis() {
// for all pairs: one of each pair has to be checked
boolean shouldStartNextActivity = (radioButton.isChecked() || radioButton2.isChecked())
&& (radioButton3.isChecked() || radioButton4.isChecked());

if (shouldStartNextActivity){
Intent intent = new Intent(this, answers.class);
startActivity(intent);
} else{
Toast.makeText(getBaseContext(), "Please answer all the questions for an accurate diagnosis", Toast.LENGTH_LONG).show();
}
}

您可以使用下一个

package com.note.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.RadioGroup
import android.widget.Toast

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val radioGroup = findViewById<RadioGroup>(R.id.opciones_pago)
radioGroup.setOnCheckedChangeListener { group, checkedId ->
Toast.makeText(applicationContext," On checked change : $group",
Toast.LENGTH_SHORT).show()
}
}
}

.XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<RadioGroup
android:id="@+id/opciones_pago"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/radio_deposito"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:checked="true"
android:text="Depósito directo" />
<RadioButton
android:id="@+id/radio_paypal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Paypal" />
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>

如果你在GitHub中输入代码,就更容易理解发生了什么。

最新更新