【发布时间】:2014-03-18 16:32:12
【问题描述】:
我正在制作一个包含抑郁症测试的 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>
“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();
}
}
“结果 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>
“结果1”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);
}
}
【问题讨论】:
-
你做错了。只需创建一个 int 全局变量并根据选中的单选按钮更改其值,并将其作为整数传递给您的下一个 Activity,并使用您想要的下一个 Activity 检索。或者您可以将该 int 变量保存到 SharedPreference
标签: java android xml radio-button