Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用单选按钮创建测试/问卷-Android_Java_Android_Xml_Radio Button - Fatal编程技术网

Java 使用单选按钮创建测试/问卷-Android

Java 使用单选按钮创建测试/问卷-Android,java,android,xml,radio-button,Java,Android,Xml,Radio Button,我正在制作一个包含抑郁症测试的android应用程序。该测试由9个问题组成,每个问题有4个可能的答案。答案以单选按钮的形式出现。我需要为单选按钮指定数值,如下所示: 答案1=0答案2=1答案3=2答案4=3 在测试结束时,用户将收到一个结果,1-10可能没有抑郁,10-27可能抑郁 每个问题都在单独的页面上提出,结果页面将包含添加的每个答案的总和 有人能给我一个建议,我该如何处理这个问题吗 res/values/strings.xml代码: <?xml version="1.0" enco

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

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

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

每个问题都在单独的页面上提出,结果页面将包含添加的每个答案的总和

有人能给我一个建议,我该如何处理这个问题吗

res/values/strings.xml代码:

<?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>
<?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>
“结果1”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>

与检查每个单选按钮以查看哪个单选按钮被选中不同,您可以从选中的ID中获得唯一选中的单选按钮

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioAnswers1);
RadioButton checkedButton = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId() ))
然后,您不需要选中更改侦听器,您可以在
nextBtn
单击上获取值。当然,您可能仍然希望选中的更改侦听器查看是否选择了一个或显示toast。。。但这不是重点

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


我肯定会建议通过intent extras将结果传递给下一个活动。每个新的测试页都可以将该页的结果添加到上一页并传递新值,然后当您到达结果页时,就已经计算出总的结果分数。如果用户开始单击“上一步”按钮来更改答案,它的伸缩性很好。

您做错了。只需创建一个int全局变量,并根据选中的单选按钮更改其值,然后将其作为整数传递给下一个活动,并使用所需的下一个活动进行检索。或者您可以将该int变量保存到SharedPreferenceI我在java代码中遇到了以下错误:无法在另一个方法中定义的内部类中引用非最终变量checkedButton。我将更新问题中的代码。@AshleyKeating您需要将选中按钮变量设置为final
final RadioButton checkedButton=…
快速谷歌搜索将为您提供一些有关原因的信息。非常感谢您,您的建议确实帮了我很大的忙。我想我差不多有。我就是无法让我的静态整数变量“result”工作并将值传递给“Test2”实例?@AshleyKeating我建议使用Intent Extras通过不同的活动传递信息。看看能不能让你开始。
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);
 }
}
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioAnswers1);
RadioButton checkedButton = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId() ))