gpt4 book ai didi

java - Android Studio 中单选按钮 onClick() 的替代选项

转载 作者:行者123 更新时间:2023-12-01 17:58:09 25 4
gpt4 key购买 nike

我一直在尝试使用 RadioGroups 为每个有 4 个选项的问题制作一个测验应用程序。我已经制作了应用程序,通过维护所选正确和错误答案的指针来计算用户的分数。例如,在我的第一个 Activity 中,有 8 个问题,每个问题都有其答案的 RadioGroup。如果用户选择了正确的选项,则正确答案的指针会增加,对于错误答案也会发生同样的情况。最后,计算该 Activity 的最终分数并用于下一个 Activity 。

整个MainActivity的代码:

package com.example.myapplicationthreesubjectquiz;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.strictmode.WebViewMethodCalledOnWrongThreadViolation;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

public int correctAnsBio=0, wrongAnsBio=0;
public RadioGroup rgbq1, rgbq2, rgbq3, rgbq4, rgbq5, rgbq6, rgbq7, rgbq8;
public RadioButton rbbq1, rbbq2, rbbq3, rbbq4, rbbq5, rbbq6, rbbq7, rbbq8;
public int corbq1, corbq2, corbq3, corbq4, corbq5, corbq6, corbq7, corbq8;

Button moveToChemistry;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

rgbq1 = findViewById(R.id.rgbq1);
rgbq2 = findViewById(R.id.rgbq2);
rgbq3 = findViewById(R.id.rgbq3);
rgbq4 = findViewById(R.id.rgbq4);
rgbq5 = findViewById(R.id.rgbq5);
rgbq6 = findViewById(R.id.rgbq6);
rgbq7 = findViewById(R.id.rgbq7);
rgbq8 = findViewById(R.id.rgbq8);

moveToChemistry = findViewById(R.id.moveToChemistry);

corbq1 = 1;
corbq2 = 1;
corbq3 = 1;
corbq4 = 1;
corbq5 = 1;
corbq6 = 1;
corbq7 = 1;
corbq8 = 1;



moveToChemistry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int scoreInBio = (4*correctAnsBio)+((-1)*wrongAnsBio);
Intent intent = new Intent(MainActivity.this, com.example.myapplicationthreesubjectquiz.chemistry.class);
intent.putExtra("bioScore", scoreInBio);
startActivity(intent);

}
});


}

public void clickedBq1(View v){

int selected = -1;

switch(rgbq1.getCheckedRadioButtonId())
{
case R.id.rbbq1o1:
selected = 1;
break;
case R.id.rbbq1o2:
selected = 2;
break;
case R.id.rbbq1o3:
selected = 3;
break;
case R.id.rbbq1o4:
selected = 4;
break;
}

if(selected == corbq1)
{
correctAnsBio++;
}
else if(selected >0 && selected != corbq1)
{
wrongAnsBio++;
}


}
public void clickedBq2(View v){

int selected = -1;

switch(rgbq2.getCheckedRadioButtonId())
{
case R.id.rbbq2o1:
selected = 1;
break;
case R.id.rbbq2o2:
selected = 2;
break;
case R.id.rbbq2o3:
selected = 3;
break;
case R.id.rbbq2o4:
selected = 4;
break;
}

if(selected == corbq2)
{
correctAnsBio++;
}
else if(selected >0 && selected != corbq2)
{
wrongAnsBio++;
}

}
public void clickedBq3(View v){

int selected = -1;

switch(rgbq3.getCheckedRadioButtonId())
{
case R.id.rbbq3o1:
selected = 1;
break;
case R.id.rbbq3o2:
selected = 2;
break;
case R.id.rbbq3o3:
selected = 3;
break;
case R.id.rbbq2o4:
selected = 4;
break;
}

if(selected == corbq3)
{
correctAnsBio++;
}
else if(selected >0 && selected != corbq3)
{
wrongAnsBio++;
}

}

public void clickedBq4(View v){

int selected = -1;

switch(rgbq4.getCheckedRadioButtonId())
{
case R.id.rbbq4o1:
selected = 1;
break;
case R.id.rbbq4o2:
selected = 2;
break;
case R.id.rbbq4o3:
selected = 3;
break;
case R.id.rbbq4o4:
selected = 4;
break;
}

if(selected == corbq4)
{
correctAnsBio++;
}
else if(selected >0 && selected != corbq4)
{
wrongAnsBio++;
}

}
public void clickedBq5(View v){

int selected = -1;

switch(rgbq5.getCheckedRadioButtonId())
{
case R.id.rbbq5o1:
selected = 1;
break;
case R.id.rbbq5o2:
selected = 2;
break;
case R.id.rbbq5o3:
selected = 3;
break;
case R.id.rbbq5o4:
selected = 4;
break;
}

if(selected == corbq5)
{
correctAnsBio++;
}
else if(selected >0 && selected != corbq5)
{
wrongAnsBio++;
}

}
public void clickedBq6(View v){

int selected = -1;

switch(rgbq6.getCheckedRadioButtonId())
{
case R.id.rbbq6o1:
selected = 1;
break;
case R.id.rbbq6o2:
selected = 2;
break;
case R.id.rbbq6o3:
selected = 3;
break;
case R.id.rbbq6o4:
selected = 4;
break;
}

if(selected == corbq6)
{
correctAnsBio++;
}
else if(selected >0 && selected != corbq6)
{
wrongAnsBio++;
}

}
public void clickedBq7(View v){

int selected = -1;

switch(rgbq7.getCheckedRadioButtonId())
{
case R.id.rbbq7o1:
selected = 1;
break;
case R.id.rbbq7o2:
selected = 2;
break;
case R.id.rbbq7o3:
selected = 3;
break;
case R.id.rbbq7o4:
selected = 4;
break;
}

if(selected == corbq7)
{
correctAnsBio++;
}
else if(selected >0 && selected != corbq7)
{
wrongAnsBio++;
}

}
public void clickedBq8(View v){

int selected = -1;

switch(rgbq8.getCheckedRadioButtonId())
{
case R.id.rbbq8o1:
selected = 1;
break;
case R.id.rbbq8o2:
selected = 2;
break;
case R.id.rbbq8o3:
selected = 3;
break;
case R.id.rbbq8o4:
selected = 4;
break;
}

if(selected == corbq8)
{
correctAnsBio++;
}
else if(selected >0 && selected != corbq8)
{
wrongAnsBio++;
}

}
}

现在的问题是,如果用户先选择了错误的选项,然后将其更改为正确的选项,则错误答案和正确答案的指针都会递增。我在 RadioButtons 的 XML 代码中使用了 onClick 属性。也许,必须有一些其他属性来仅显示获取最终选中选项的选项,而不是一次又一次地发生。

以下是 XML 代码:

<RadioGroup
android:id="@+id/rgbq1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp">

<RadioButton
android:id="@+id/rbbq1o1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimary"
android:onClick="clickedBq1"
android:text="Option1"
android:textColor="@color/colorPrimaryVariant"
android:textSize="20sp" />

最佳答案

您可以添加单选按钮更改监听器

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);        
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
}
})

或在单击下一个按钮时选择单选按钮。像下面这样

    next.setOnClickListener(new OnClickListener(View v){
String value =
((RadioButton)findViewById(rg.getCheckedRadioButtonId()))
.getText().toString();
// here you can compare the selected answer is correct or not
});

关于java - Android Studio 中单选按钮 onClick() 的替代选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60680932/

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