2013-04-03 98 views
0

我有一個包含8個單選按鈕的4個活動的simpe表單。當一個活動被回答時,它會進入下一個活動,並且我希望在第四個活動中顯示已檢查的單選按鈕的結果。有人可以幫我添加單選按鈕的值並計算答案嗎? 我想創建一個心理學測試,每個單選按鈕必須有不同的值。將它們加起來會形成獨特的心理學概況。 以下是我的應用程序中的一個佈局和活動。 我是新來的,我很感激所有的幫助。謝謝!如何將值添加到單選按鈕並計算答案?

佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 


<TextView 
android:id="@+id/textView" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/question_1"/> 

<RadioGroup 
android:id="@+id/radiogroup"  
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<RadioButton 
android:id="@+id/radiobutton1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/radio_1"/> 

<RadioButton 
android:id="@+id/radiobutton2" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/radio_2"/> 
</RadioGroup> 

</LinearLayout> 

活動:

package com.example.app_test; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.RadioButton; 

public class MainActivity extends Activity { 

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

     RadioButton buttonOne = (RadioButton)findViewById(R.id.radiobutton1); 

     buttonOne.setOnCheckedChangeListener(new OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
      { 
       if (isChecked) 
       { 
        //Go to the activity for button 1 here 
        MainActivity.this.startActivity(new Intent(MainActivity.this, SecondActivity.class)); 
       } 
      } 
     }); 

     RadioButton buttonTwo = (RadioButton)findViewById(R.id.radiobutton2); 

     buttonTwo.setOnCheckedChangeListener(new OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
      { 
       if (isChecked) 
       { 
        //Go to the activity for button 2 here 
        MainActivity.this.startActivity(new Intent(MainActivity.this, SecondActivity.class)); 
       } 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

回答

0

嘗試建立在每一個活動的靜態變量和最終activity.this訪問它們是單向的。

+0

但是如何在單擊一個按鈕時創建靜態變量?謝謝! – eeschimosu

0

你有一些選擇。

1 - 最簡單的是基於只使用一個活動,4 LinearLayout中,每一個問題,把它在一個行,改變它的知名度。在第四個結尾,你可以總結沒有問題。

2 - 另一種選擇是使用活動。當用戶完成每個問題組時,您必須爲下一個傳遞Bundle中單選按鈕值的活動指定一個意圖,如果沒有,您將丟失該值,因爲您無法訪問其他活動值(您應該通過它成捆,或將其保存在數據庫中,sharedPreferences或存儲系統...

3 - 有這兩種解決方案之間的hibryd,使用碎片,但我建議你使用的第一個解決方案

0

。這似乎是錯誤的做法,如果你有20個問題你會啓動20個活動。

你也許可以逃脫一個活動。這應該讓你在正確的道路上(儘管它是不完整的一個天真)。

做一個QuestionActivity,有像(以下僞代碼)的佈局:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <TextView 
     android:id="@+id/question_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    <RadioGroup 
     android:id="@+id/answer_group" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <RadioButton 
      android:id="@+id/answer1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 
     ... 
    </RadioGroup> 
</LinearLayout> 

然後在你的QuestionActivity你會碰到這樣的:

public class QuestionActivity extends Activity { 

    private int[] answers = new int[MAX_QUESTIONS]; 
    private int currentQuestionIndex = 0; 

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

     RadioGroup answerGroup = (RadioGroup)findViewById(R.id.answer_group); 

     answerGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) 
      { 
       answers[currentQuestionIndex] = checkedId; 
       QuestionActivity.this.loadQuestionAtIndex(++currentQuestionIndex); 
      } 
     }); 

    private void loadQuestionAtIndex(int index) { 
     if (index == MAX_QUESTIONS) { 
      computeResults(); // this method would traverse the answers array to tally the results (based on an answer key, or similar) 
      return; 
     } 
     // get the question text from an array resource or some other data source 
     // update the question text and radio buttons text 
    } 
} 

我希望讓你去在正確的方向。如果您的問題需要交互超越文本(即一些問題會顯示視頻),你應該考慮使用Fragment類(http://developer.android.com/guide/components/fragments.html)創建UI和業務邏輯(VideoQuestionFragment,TextQuestionFragment等)的可重複使用的部分。

相關問題