You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android测验APP开发:如何计算单选按钮数值总和并在新布局显示结果?

实现测验APP的分数计算与结果跳转功能

看起来你想要做一个能计算用户单选选项总分,然后跳转到结果页展示的测验APP对吧?我来一步步帮你实现这个功能:

需求回顾

  • 计算用户选中的所有RadioButton对应的数值总和
  • 点击"生成结果"按钮后,跳转到独立的结果页面展示总分

1. 完善问题页布局(activity_main.xml)

你提供的代码没写完,我先帮你补全第三个问题的布局,并且添加"生成结果"按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:id="@+id/instruction_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请完成以下题目,每题选择1-7的选项"
        android:textSize="18sp"
        android:marginBottom="20dp"/>

    <!-- Question 1 -->
    <TextView
        android:id="@+id/question1_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="问题1:你对当前状态的满意度?"
        android:textSize="16sp"
        android:marginBottom="8dp"/>
    <RadioGroup
        android:id="@+id/question1_radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/question1_radioButton1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />
        <RadioButton
            android:id="@+id/question1_radioButton2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />
        <RadioButton
            android:id="@+id/question1_radioButton3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />
        <RadioButton
            android:id="@+id/question1_radioButton4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />
        <RadioButton
            android:id="@+id/question1_radioButton5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />
        <RadioButton
            android:id="@+id/question1_radioButton6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" />
        <RadioButton
            android:id="@+id/question1_radioButton7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />
    </RadioGroup>

    <!-- Question 2 -->
    <TextView
        android:id="@+id/question2_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="问题2:你愿意推荐该产品的程度?"
        android:textSize="16sp"
        android:marginTop="20dp"
        android:marginBottom="8dp"/>
    <RadioGroup
        android:id="@+id/question2_radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/question2_radioButton1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />
        <RadioButton
            android:id="@+id/question2_radioButton2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />
        <RadioButton
            android:id="@+id/question2_radioButton3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />
        <RadioButton
            android:id="@+id/question2_radioButton4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />
        <RadioButton
            android:id="@+id/question2_radioButton5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />
        <RadioButton
            android:id="@+id/question2_radioButton6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" />
        <RadioButton
            android:id="@+id/question2_radioButton7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />
    </RadioGroup>

    <!-- Question 3 -->
    <TextView
        android:id="@+id/question3_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="问题3:你再次使用的可能性?"
        android:textSize="16sp"
        android:marginTop="20dp"
        android:marginBottom="8dp"/>
    <RadioGroup
        android:id="@+id/question3_radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/question3_radioButton1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />
        <RadioButton
            android:id="@+id/question3_radioButton2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />
        <RadioButton
            android:id="@+id/question3_radioButton3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />
        <RadioButton
            android:id="@+id/question3_radioButton4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />
        <RadioButton
            android:id="@+id/question3_radioButton5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />
        <RadioButton
            android:id="@+id/question3_radioButton6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" />
        <RadioButton
            android:id="@+id/question3_radioButton7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />
    </RadioGroup>

    <!-- 生成结果按钮 -->
    <Button
        android:id="@+id/generate_result_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="生成结果"
        android:layout_marginTop="30dp"
        android:textSize="18sp"/>

</LinearLayout>

2. 创建结果页布局(activity_result.xml)

这个布局用来展示最终的总分:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="16dp">

    <TextView
        android:id="@+id/result_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你的测验总分是:"
        android:textSize="20sp"
        android:marginBottom="20dp"/>

    <TextView
        android:id="@+id/total_score_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="36sp"
        android:textColor="@android:color/holo_blue_dark"/>

    <Button
        android:id="@+id/back_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回重新答题"
        android:layout_marginTop="30dp"/>

</LinearLayout>

3. 编写MainActivity逻辑(处理选择、计算总分、跳转)

Kotlin版本

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.RadioGroup
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val generateBtn = findViewById<Button>(R.id.generate_result_btn)
        generateBtn.setOnClickListener {
            // 计算每个问题的得分
            val score1 = getSelectedScore(R.id.question1_radiogroup)
            val score2 = getSelectedScore(R.id.question2_radiogroup)
            val score3 = getSelectedScore(R.id.question3_radiogroup)

            val totalScore = score1 + score2 + score3

            // 跳转到结果页,传递总分
            val intent = Intent(this, ResultActivity::class.java)
            intent.putExtra("TOTAL_SCORE", totalScore)
            startActivity(intent)
        }
    }

    // 获取单个RadioGroup的选中分数
    private fun getSelectedScore(radioGroupId: Int): Int {
        val radioGroup = findViewById<RadioGroup>(radioGroupId)
        val selectedId = radioGroup.checkedRadioButtonId
        return if (selectedId != -1) {
            // 根据选中的RadioButton的text获取数值
            val radioBtn = findViewById<android.widget.RadioButton>(selectedId)
            radioBtn.text.toString().toInt()
        } else {
            // 如果没选,默认得0分(你也可以改成提示用户选择)
            0
        }
    }
}

Java版本

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

        Button generateBtn = findViewById(R.id.generate_result_btn);
        generateBtn.setOnClickListener(v -> {
            int score1 = getSelectedScore(R.id.question1_radiogroup);
            int score2 = getSelectedScore(R.id.question2_radiogroup);
            int score3 = getSelectedScore(R.id.question3_radiogroup);

            int totalScore = score1 + score2 + score3;

            Intent intent = new Intent(MainActivity.this, ResultActivity.class);
            intent.putExtra("TOTAL_SCORE", totalScore);
            startActivity(intent);
        });
    }

    private int getSelectedScore(int radioGroupId) {
        RadioGroup radioGroup = findViewById(radioGroupId);
        int selectedId = radioGroup.getCheckedRadioButtonId();
        if (selectedId != -1) {
            RadioButton radioBtn = findViewById(selectedId);
            return Integer.parseInt(radioBtn.getText().toString());
        } else {
            return 0;
        }
    }
}

4. 编写ResultActivity逻辑(展示总分)

Kotlin版本

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class ResultActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_result)

        // 获取传递的总分
        val totalScore = intent.getIntExtra("TOTAL_SCORE", 0)
        val scoreText = findViewById<TextView>(R.id.total_score_text)
        scoreText.text = totalScore.toString()

        // 返回按钮逻辑
        val backBtn = findViewById<Button>(R.id.back_btn)
        backBtn.setOnClickListener {
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }
    }
}

Java版本

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class ResultActivity extends AppCompatActivity {

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

        int totalScore = getIntent().getIntExtra("TOTAL_SCORE", 0);
        TextView scoreText = findViewById(R.id.total_score_text);
        scoreText.setText(String.valueOf(totalScore));

        Button backBtn = findViewById(R.id.back_btn);
        backBtn.setOnClickListener(v -> {
            startActivity(new Intent(ResultActivity.this, MainActivity.class));
            finish();
        });
    }
}

最后一步:注册ResultActivity

在AndroidManifest.xml的<application>标签里添加ResultActivity的注册:

<activity
    android:name=".ResultActivity"
    android:exported="false"/>

这样整个功能就完成了:用户选择每个问题的选项后,点击生成结果按钮,就会计算所有选中选项的数值总和,跳转到结果页展示,还能返回重新答题。

内容的提问来源于stack exchange,提问作者Ruzain Irfan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:57:08