Last active
April 2, 2016 06:22
-
-
Save AraujoJordan/49b1e21c36f022e530ce to your computer and use it in GitHub Desktop.
[Nova Cursos] Aula6 QuizFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package araujo.jordan.novaquiz; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.RadioButton; | |
import android.widget.RadioGroup; | |
import android.widget.TextView; | |
/** | |
* Created by Jordan on 12/03/2016. | |
*/ | |
public class QuizFragment extends Fragment { | |
private int numPergunta; | |
private Quiz quiz; //Classe Quiz | |
private View view; | |
private int selecinado = 1; | |
private TextView perguntaNumTV; | |
private TextView perguntaTV; | |
private RadioButton resposta1; | |
private RadioButton resposta2; | |
private RadioButton resposta3; | |
private RadioButton resposta4; | |
private RadioButton resposta5; | |
private RadioGroup group; | |
/** | |
* Lembre-se que Activitys e Fragments não podem ter Construtores, | |
* logo precisamos preenche-las depois que elas são criadas | |
* Aqui preenche o quiz com as perguntas que serão feitas | |
* | |
* @param numPergunta o numero da pergunta (a MainActivity que dirá) | |
* @param quiz a pergunta | |
*/ | |
public void addValores(int numPergunta, Quiz quiz) { | |
this.numPergunta = numPergunta; | |
this.quiz = quiz; | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
this.view = inflater.inflate(R.layout.fragment_quiz, container, false); //Diz a fragment para usar o fragment_quiz.xml como layout | |
perguntaNumTV = (TextView) view.findViewById(R.id.numPergunta); | |
perguntaTV = (TextView) view.findViewById(R.id.pergunta); | |
resposta1 = (RadioButton) view.findViewById(R.id.radioButton); | |
resposta2 = (RadioButton) view.findViewById(R.id.radioButton2); | |
resposta3 = (RadioButton) view.findViewById(R.id.radioButton3); | |
resposta4 = (RadioButton) view.findViewById(R.id.radioButton4); | |
resposta5 = (RadioButton) view.findViewById(R.id.radioButton5); | |
group = (RadioGroup) view.findViewById(R.id.radioGroup); | |
return view; | |
} | |
/** | |
* Fragments são um pouco diferentes de activitys neste ponto, seus elementos só são visíveis ao usuário | |
* após ela aparecer na tela, logo precisamos preencher a fragment apenas quando temos certeza | |
* que os elementos são visíveis pro usuário. | |
*/ | |
@Override | |
public void onResume() { | |
super.onResume(); | |
perguntaNumTV.setText(String.valueOf(this.numPergunta)); | |
perguntaTV.setText(quiz.getPergunta()); | |
resposta1.setText(quiz.getResposta()[0]); | |
resposta2.setText(quiz.getResposta()[1]); | |
resposta3.setText(quiz.getResposta()[2]); | |
resposta4.setText(quiz.getResposta()[3]); | |
resposta5.setText(quiz.getResposta()[4]); | |
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(RadioGroup group, int checkedId) { | |
switch (checkedId) { | |
case R.id.radioButton: | |
selecinado = 1; | |
break; | |
case R.id.radioButton2: | |
selecinado = 2; | |
break; | |
case R.id.radioButton3: | |
selecinado = 3; | |
break; | |
case R.id.radioButton4: | |
selecinado = 4; | |
break; | |
case R.id.radioButton5: | |
selecinado = 5; | |
break; | |
} | |
} | |
}); | |
} | |
/** | |
* Método para saber se o usuário acertou ou não a resposta | |
* | |
* @return se ele acertou (true) ou não (false) | |
*/ | |
public boolean acertou() { | |
if (selecinado-1 == quiz.getRespostaCorreta()) //ou return selecionado==respostaCorreta; | |
return true; | |
else | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment