使用Radio-Buttons创建测试/问卷调查 - Android

时间:2014-02-18 18:08:04

标签: java android xml radio-button

我正在制作一个包含抑郁症测试的Android应用程序。测试由9个问题组成,每个问题有4个可能的答案。答案是单选按钮的形式。我需要为单选按钮分配数值,如下所示:

答案1 = 0答案2 = 1答案3 = 2答案4 = 3

在测试结束时,用户将收到一个结果,1-10可能没有被压抑,10-27可能会被压抑。

每个问题都在一个单独的页面上询问,结果页面将包含结果,即每个答案的总和。

有人可以就我应该如何解决这个问题提出建议吗?

res / values / strings.xml code:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<integer name="int1">0</integer>
<integer name="int2">1</integer>
<integer name="int3">2</integer>
<integer name="int4">3</integer>

</resources>

“Test1”xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" 
tools:context=".Test1">

<!-- title text -->
<TextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp"
    android:text="@string/D1title"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<!-- subtitle text -->
<TextView
    android:id="@+id/txtSubTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtTitle"
    android:layout_centerHorizontal="true"
    android:text="@string/Q1Subtitle"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<!-- question 1 text -->
<TextView
    android:id="@+id/txtQ1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txtSubTitle"
    android:layout_marginTop="38dp"
    android:text="@string/Q1"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<!-- radio group to allow for only one radio button selection -->
<RadioGroup
    android:id="@+id/radioAnswers1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtQ1" >

     <!-- 1st answer radio button-->
    <RadioButton
    android:id="@+id/radioA1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="48dp"
    android:checked="false"
    android:text="@string/zero"
    android:tag="@integer/int1" />

<!-- 2nd answer radio button-->
<RadioButton
    android:id="@+id/radioA2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/one"
    android:tag="@integer/int2" />

<!-- 3rd answer radio button-->
<RadioButton
    android:id="@+id/radioA3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/two"
    android:tag="@integer/int3" />

<!-- 4th answer radio button-->
  <RadioButton
    android:id="@+id/radioA4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/three"
    android:tag="@integer/int4" />

</RadioGroup>

 <!-- next button -->
<Button
    android:id="@+id/btnNext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/txtTitle"
    android:text="@string/next" />

</RelativeLayout>

“Test1”java代码:

package com.lifematters;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class Test1 extends Activity {

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

    //define Navigation Image Buttons
    final Button nextBtn = (Button) findViewById(R.id.btnNext);

    final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioAnswers1);
    final RadioButton checkedButton = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId()));

    static int result;




    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged (RadioGroup group, int checkedId){

        final RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioA1);
        final RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioA2);
        final RadioButton radioButton3 = (RadioButton) findViewById(R.id.radioA3);

        if (radioButton1.isChecked()) {
            displayToast("No, not at all");

        } else if (radioButton2.isChecked()) {
            displayToast("On some days");

        }else if (radioButton3.isChecked()) {
            displayToast("On more than half the days");

        }else {
            displayToast("Nearly everyday");
        }
    }

});


  //Set up listener for Test
   nextBtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
            //listener call this function

           checkedButton.getTag(); 

           Intent intent = new Intent(getBaseContext(), Test2.class);
           intent.putExtra("EXTRA_RESULT", result);
           startActivity(intent);

           openTest2();
        }
    });


  }

    //Open test page
    public void openTest2() {
    //create new textview
    Intent i = new Intent(getApplicationContext(), Test2.class);
    startActivity(i);
  }


  public void displayToast(String text){
  Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}

}

“Results1”XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" >

<!-- Creating Results Title -->
<TextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="19dp"
    android:text="@string/resultsTitle"
    android:textAppearance="?android:attr/textAppearanceLarge" />

 <!-- Creating Results Subtitle -->
 <TextView
    android:id="@+id/txtSubtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtTitle"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:text="@string/subtitleR1" />


<!-- Creating Score Label  -->
 <TextView
    android:id="@+id/txtScore"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/txtResults"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="29dp"
    android:text="@string/score1"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<!-- Creating Results Text -->
<TextView
    android:id="@+id/txtResults"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnNext"
    android:layout_alignParentLeft="true"
    android:text="@string/results1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<!-- Creating Next Button -->
<Button
    android:id="@+id/btnNext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginRight="14dp"
    android:text="@string/next" />

</RelativeLayout>

“Results1”java代码:

package com.lifematters;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;

public class Results1 extends Activity {

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

    final Button nextBtn = (Button) findViewById(R.id.btnNext);
            final TextView score = (TextView) findViewById(R.id.txtScore);

            int result;

    result = getText(Test1.result1);


//Set up listener for Next
nextBtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
            //listener call this function
            open101();
        }
    });


 }

//Open test page
public void open101() {
//create new textview
Intent i = new Intent(getApplicationContext(), Depression101.class);
 startActivity(i);
 }
}

1 个答案:

答案 0 :(得分:1)

您可以从选中的ID中获取唯一一个选中的单选按钮,而不是检查每个单选按钮以查看哪一个单选按钮。

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioAnswers1);
RadioButton checkedButton = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId() ))

然后您不需要选中已更改的更改侦听器,您可以在nextBtn单击时获取值。当然,您可能仍然希望已检查的更改侦听器查看是否已选择一个或显示toast ...但这不是重点。

第2部分,如何将该单选按钮转换为数字值。真的,有很多方法可以做到这一点。我认为我的首选方式是枚举,但你可以为字符串到int映射,单选按钮id到int映射等做任意数量的方法。你甚至可以使用标签。看起来您在标签周围有一些注释代码。我只是在xml中设置标签,然后在准备好进入下一页时使用checkedButton.getTag()来获取值。很简单。

我肯定会建议通过意图附加内容将结果传递给下一个Activity。每个新的测试页面都可以将该页面的结果添加到上一页并传递新值,然后当您到达结果页面时,已经计算了总结果分数。如果用户开始单击后退按钮来更改答案,它会很好地缩放。