2017-07-19 17 views
-2

我有興趣爲30個人物形象問題製作Android版APP,其中每個問題都有兩種選擇。我想過使用RadioButton,但由於有30個問題我不想一次性將它們全部包含在屏幕上,我想只顯示一個問題和兩個備選方案,並且每個備選方案的每個選擇都已被稱爲另一個問題。如何在數組中使用單選按鈕?

是否可以在不創建30個活動的情況下執行此操作?

我看到它可能做數組,但我不知道如何從一個問題運行到另一個問題。

非常感謝你!

+0

爲什麼你重複自己2次? – sudo

+0

我不這麼認爲創建30個活動很好,爲什麼不一次只顯示1個問題,並在用戶選擇正確的答案後更改問題,這將更有效率。 –

回答

0

此代碼實現所需的功能。這是非常基本的,可以根據您的需要進行改進。只需將此活動和佈局添加到您的項目。

Java文件:此代碼使用單個佈局並重新對單選按鈕下一個問題的看法點擊

public class QuestionsActivity extends AppCompatActivity implements 
    RadioGroup.OnCheckedChangeListener{ 
    LinkedHashMap<String,RadioGroup> questionList; 
    LinkedHashMap<String,String> answerList; 
    ArrayList<String> keys=new ArrayList<>(); 
    int keyCounter=0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_questions); 
     questionList=new LinkedHashMap<>(); //This Map contains all the questions with two radio button (options) 
     answerList=new LinkedHashMap<>(); //This Map will contain question along with selected answer. 

     initQuestions(); //This method will add 30 questions with options 

     keys.addAll(questionList.keySet()); 

     showQuestions(keys.get(keyCounter));//This method will show the first question 






    } 

    private void showQuestions(String key) { 
     TextView textView=(TextView)findViewById(R.id.tv_question); 
     textView.setText(key); 
     LinearLayout layout =(LinearLayout)findViewById(R.id.questionsLayout); 
     layout.removeAllViews(); 
     RadioGroup rg=questionList.get(key); 
     rg.setOrientation(LinearLayout.HORIZONTAL); 
     layout.addView(rg); 
     rg.setOnCheckedChangeListener(this); 


    } 

    private void initQuestions() { 
     for (int i = 1; i <=3; i++) { 
      RadioGroup rg=new RadioGroup(this); 
      RadioButton rb1 =new RadioButton(this); 
      rb1.setText("Q "+i+" RadioButton 1"); 
      RadioButton rb2 =new RadioButton(this); 
      rb2.setText("Q "+i+" RadioButton 2"); 
      rg.addView(rb1);rg.addView(rb2); 
      questionList.put("Question "+i,rg); 


     } 
    } 

    @Override 
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { 
     RadioButton rb=(RadioButton) findViewById(group.getCheckedRadioButtonId()); 




     if(keyCounter<questionList.size()) { 
      answerList.put(keys.get(keyCounter),rb.getText().toString()); // Putting the question and selected answer to 'answerList' map. 
      keyCounter++; 
      if(keyCounter<questionList.size()) { 
       showQuestions(keys.get(keyCounter));// showing the next question. 
      } 

     }else { 
      Toast.makeText(this, "You've answered all the questions.", Toast.LENGTH_SHORT).show(); 
     } 

     for (String s : answerList.keySet()) { 
      System.out.println("Q--> "+s+", A--> "+answerList.get(s)); // Here you can see all the questions and selected answers on your logs(AndroidMonitor). 
     } 
    } 
    } 

佈局文件:此文件包含將被用於顯示的問題一個TextView和包含RadioGroup的線性佈局

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
    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:orientation="vertical" 
    tools:context="edios.endlessscrollrecycler.QuestionsActivity"> 

     <TextView 
      android:id="@+id/tv_question" 
      android:layout_marginTop="10dp" 
      android:padding="5dp" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:id="@+id/questionsLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"></LinearLayout> 


</LinearLayout> 
相關問題