2014-02-18 24 views
1

我正在製作一個包含抑鬱症測試的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); 
} 
} 
+0

你做錯了。只需創建一個int全局變量,並根據選中的單選按鈕更改其值,並將其作爲整數傳遞給下一個活動,並使用您想要的下一個活動進行檢索。或者你可以將該int變量保存爲** SharedPreference ** – Piyush

回答

1

而不是檢查每一個單選按鈕,看看哪一個被選中,就可以得到只能從檢查的ID檢查單選按鈕。

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

然後你不需要檢查更改偵聽器,你可以得到nextBtn點擊的值。當然,你可能仍然希望被選中的變更監聽者看看是否被選中或顯示吐司......但除此之外。

第2部分,如何將該單選按鈕轉換爲數字值。真的,有很多方法可以做到這一點。我認爲我的首選方式是一個枚舉,但是你可以爲字符串到整型映射,單選按鈕ID到整型映射等等做任何方法。你甚至可以使用標籤。看起來你在標籤周圍有一些評論代碼。我只需在xml中設置標籤,然後使用checkedButton.getTag()獲得準備轉到下一頁時的值。夠簡單。

我肯定會推薦通過意向演員將結果傳遞給下一個活動。每個新的測試頁面都可以將該頁面的結果添加到上一頁面並傳遞新值,然後當您到達結果頁面時,總計結果得分已經計算完畢。如果用戶開始點擊後退按鈕來更改答案,則可以很好地進行縮放。

+0

我在我的java代碼中得到這個錯誤:不能引用在不同方法中定義的內部類中的非最終變量checkedButton。我將在問題中更新我的代碼。 –

+0

@AshleyKeating你需要讓你選中的按鈕變量final'final RadioButton checkedButton = ...'快速谷歌搜索會給你一些信息,爲什麼。 – jacobhyphenated

+0

非常感謝,你的建議對我很有幫助。我想我多多少少有它。我只是不能讓我的靜態整數變量「結果」工作,並將值帶到「Test2」實例? –