Android 如何向单选按钮添加值并计算答案?

Android 如何向单选按钮添加值并计算答案?,android,eclipse,radio-button,Android,Eclipse,Radio Button,我有一个simpe表单,包含4个活动,其中包含8个单选按钮。当一个活动被应答时,它将转到下一个活动,我希望在第四个活动中显示选中单选按钮的结果。有人能帮我在单选按钮上添加值并计算答案吗? 我想创建一个心理健康测试,每个单选按钮必须有不同的值。把它们加起来会产生一个独特的心理特征。 下面是我的应用程序中的一个布局和活动。 我是新手,我很感激能得到的所有帮助。谢谢大家! 布局: <LinearLayout xmlns:android="http://schemas.android.co

我有一个simpe表单,包含4个活动,其中包含8个单选按钮。当一个活动被应答时,它将转到下一个活动,我希望在第四个活动中显示选中单选按钮的结果。有人能帮我在单选按钮上添加值并计算答案吗? 我想创建一个心理健康测试,每个单选按钮必须有不同的值。把它们加起来会产生一个独特的心理特征。 下面是我的应用程序中的一个布局和活动。 我是新手,我很感激能得到的所有帮助。谢谢大家!

布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>


<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question_1"/>

<RadioGroup 
android:id="@+id/radiogroup"    
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<RadioButton
android:id="@+id/radiobutton1"  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/radio_1"/>

<RadioButton
android:id="@+id/radiobutton2"  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/radio_2"/>
</RadioGroup>  

</LinearLayout>

尝试在每个活动中创建一个静态变量,并在最终活动中访问它们。这是一种方法。

您有一些选择

1-最简单的方法是只使用一个活动,4个线性布局,每个问题一个,将其排成一行并更改其可见性。在第四节末尾,你可以毫无问题地求和

2-另一种选择是使用活动。当用户完成每个问题组时,您必须启动下一个活动的意图,并将单选按钮值传递到捆绑包中,否则,您将丢失此值,因为您无法访问其他活动值(您应将其作为捆绑包传递,或将其保存在数据库、共享参考资料或存储系统中


3-这两个解决方案之间存在一个hibryd,使用片段,但我建议您使用第一个解决方案。

这似乎是错误的方法,如果您有20个问题,您将开始20个活动

你可能只做一项活动就可以逃脱惩罚。这应该会让你走上正确的道路(尽管这是不完整和幼稚的)

制作一个
QuestionActivity
,其布局如下(下面的伪代码):


我希望这能让你朝正确的方向前进。如果你的问题需要超越文本的交互性(即一些问题会显示一个视频),你应该考虑使用<代码>片段< /Cord>类()来创建可重用的UI和业务逻辑片(VideoDebug片段,TrimeDebug片段等)。.

但单击一个按钮时如何创建静态变量?谢谢!
package com.example.app_test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

public class MainActivity extends Activity {

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

      RadioButton buttonOne = (RadioButton)findViewById(R.id.radiobutton1);

        buttonOne.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    //Go to the activity for button 1 here
                    MainActivity.this.startActivity(new Intent(MainActivity.this, SecondActivity.class));
                }
            }
        });

        RadioButton buttonTwo = (RadioButton)findViewById(R.id.radiobutton2);

        buttonTwo.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    //Go to the activity for button 2 here
                    MainActivity.this.startActivity(new Intent(MainActivity.this, SecondActivity.class));
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/question_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <RadioGroup
        android:id="@+id/answer_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/answer1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        ...
     </RadioGroup>
</LinearLayout>
public class QuestionActivity extends Activity {

    private int[] answers = new int[MAX_QUESTIONS];
    private int currentQuestionIndex = 0;

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

        RadioGroup answerGroup = (RadioGroup)findViewById(R.id.answer_group);

        answerGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                answers[currentQuestionIndex] = checkedId;
                QuestionActivity.this.loadQuestionAtIndex(++currentQuestionIndex);
            }
        });

    private void loadQuestionAtIndex(int index) {
        if (index == MAX_QUESTIONS) {
            computeResults(); // this method would traverse the answers array to tally the results (based on an answer key, or similar)
            return;
         }
        // get the question text from an array resource or some other data source
        // update the question text and radio buttons text
    }
}