我正在製作一個包含抑鬱症測試的Android應用程序。該測試包含9個問題,每個問題有4個可能的答案。答案以單選按鈕的形式出現。我需要的數值分配給單選按鈕如下:使用無線電按鈕創建測試/問卷 - Android
回答1 = 0回答2 = 1回答3 = 2回答4 = 3
在試驗結束時,用戶將收到一個結果, 1-10可能不會沮喪,10-27可能會沮喪。
每個問題都在一個單獨的頁面上提問,結果頁面將包含結果,即每個添加的答案的總數。
有人可以提出一個建議,我應該如何解決這個問題?
RES /值/ 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>
「測試1」 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>
「測試1」 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全局變量,並根據選中的單選按鈕更改其值,並將其作爲整數傳遞給下一個活動,並使用您想要的下一個活動進行檢索。或者你可以將該int變量保存爲** SharedPreference ** – Piyush