2013-10-29 95 views
0

我有两个片段和第一个片段我有两个页面,每个页面至少有两个单选按钮的问题,这些单选按钮将选择您的答案。 目前,我必须使用if else功能并使用setText设置单选按钮的问题和答案,并且单击正确的答案时,只有当您转到最后一页并单击显示结果时,才会显示任何内容。 现在我必须继续使用if else,并且使用setOnClickListener。 另外,通过使用setOnClickListener,它会向我的结果页面的变量发送+1结果。有没有更简单的方法来做到这一点? radiogroups /按钮 - android

有没有更简单的方法来做到这一点?

这是我的代码看起来像

if(fragmentNumber == 0) 
    { 
     ask_question.setText("Question1?"); 
     rb1.setText("Answer1"); 
     rb2.setText("Answer2"); 
     rb1.setOnClickListener(new android.view.View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       ResultsFragment.Q1 = 1; 
      } 
     }); 
    } 
    else if(fragmentNumber == 1) 
    { 
     ask_question.setText("Question2?"); 
     rb1.setText("Answer2-1"); 
     rb2.setText("Answer2-2"); 
     rb2.setOnClickListener(new android.view.View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       ResultsFragment.Q2 = 1; 
      } 
     }); 

    } 

除了寻找一种更简单的方法来做到这一点,我遇到了另一个问题了。 让我用例子。

E.g.如果第一季度我得到了正确的结果,第二季度我得到了错误,当我去显示结果我会得到1/2这是很好的,但如果我回到第一季度并故意点击错误的答案然后回去显示结果,它仍然会显示1/2而不是0/2,这意味着如果将1设置为变量,那么它会留在那里。我知道如果我使用setOnclickListener,那么我可以设置其他radioButtons == 0,但这意味着每个if语句中都会有LOTS setonClickListner,这是我寻求更简单结果的另一个原因。

在此先感谢。

回答

1

寻找更简单的方法?好。在我看来,你应该有一个片段(自定义QAFragment)只有一个页面的问题和答案。

public class QuestionFragment extends Fragment { 

    public static QuestionFragment newInstance(String questionText, String[] answerArray) { 
     QuestionFragment f = new QuestionFragment(); 

     Bundle args = new Bundle(); 
     args.putString("questionText", questionText); 
     args.putStringArray("answerArray", answerArray); 

     f.setArguments(args); 

     return f; 
    } 

    public String getQuestionText() { 
     return getArguments().getString("questionText"); 
    } 

    public String[] getAnswerArray() { 
     return getArguments().getStringArray("answerArray"); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View content = null; 
     // TODO: inflate your content here, put your radios and question text 

     final String questionText = getQuestionText(); 
     String[] answerArray = getAnswerArray(); 

     // TODO: set your question text with questionText value 
     // TODO: set your asnwers with answerArray values 

     RadioGroup radioGroup = null; 
     // TODO: keep your radios with this radioGroup 

     radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup radioGroup, int radioButtonID) { 
       String selectedAnswer = null; 
       // TODO: if/else or switch statement on radioButtonId to get the selected answer 

          // Post the result to main activity to do extra operations or open new page. 
       ((MainActivity)getActivity()).postQAResult(questionText, selectedAnswer); 
      } 
     }); 

     return content; 
    } 
} 

postQAResult方法添加到您的主要活动以侦听片段中的回调。

public void postQAResult(String question, String asnwer) { 
    // TODO: handle answer for the question. 
    // For example if you need to store answer you can use SharedPreferences to save. 
    // Or you can ask different questions for given answer. 
} 

现在,您可以在需要新问题/答案页面时从活动中添加此qa片段。例如;

String questionText = "Any problem?"; 
    String[] answerArray = new String[]{"Yes","No"}; 
    QuestionFragment questionFragment = QuestionFragment.newInstance(questionText, answerArray); 

    getFragmentManager().beginTransaction().add(android.R.id.content, questionFragment, "UNIQUE_TAG_FOR_YOUR_PAGE").commit(); 

您可能需要向用户显示上一个问题。然后,不要删除你的qa片段,并在添加它们时将它们放入backstack中。

+0

谢谢我对这件事还是相当新的...... – WXR

+0

所以,让我们说如果我只是复制并粘贴你的代码......你给了我三个部分......我相信第一部分是我创建一个QAFragment类的地方当然在布局中,我应该创建一个像一个带有几个TextView和RadioGroup的qa.xml页面,然后像'RadioGroup gb =(RadioGroup)findViewById(R.id.radio1)'然后'setText'一样实现到你的TODO中? – WXR

+0

你的第二个代码尤其如此。 'SharedPreferences'部分我不太明白:(对不起 – WXR

相关问题