Android Studio中单选按钮的onClick()的替代选项
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio中单选按钮的onClick()的替代选项相关的知识,希望对你有一定的参考价值。
我一直在尝试使用RadioGroups开发一个具有4个选项的问题的应用程序。我制作了该应用程序,以通过为选定的正确答案和错误答案维护一个指针来计算用户的分数。例如,在我的第一个活动中,有8个问题,每个问题都有其RadioGroups作为答案。如果用户选择正确的选项,则正确答案的指针将递增,而错误答案的指针也会发生相同的情况。最后,将计算该活动的最终分数并将其用于下一个活动。
整个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++;
}
}
}
现在的问题是,如果用户先选择了不正确的选项,然后将其更改为正确的选项,则错误和正确答案的指针都会增加。我已经在RadioButton的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
});
另一答案
您必须提供一个变量来检查您之前是否回答过(先前的回答为true或false)。
方法示例clickedBq1。
public void clickedBq1(View v){
int selected = -1;
boolean statusAnswer = false
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(statusAnswer == false)
{
if(selected == corbq1)
{
correctAnsBio++;
}
else if(selected >0 && selected != corbq1)
{
wrongAnsBio++;
}
statusAnswer == true
}
else
{
if(selected == corbq1)
{
correctAnsBio++;
wrongAnsBio--;
}
else if(selected >0 && selected != corbq1)
{
wrongAnsBio++;
correctAnsBio--;
}
}
}
以上是关于Android Studio中单选按钮的onClick()的替代选项的主要内容,如果未能解决你的问题,请参考以下文章