Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.ragie.sampleactivitytofragment;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import java.util.Random;
- public class Main2Activity extends AppCompatActivity {
- Button answer1, answer2, answer3, answer4;
- TextView score, question;
- private Question mQuestion = new Question();
- private String mAnswer;
- private int mScore = 0;
- private int mQuestionLength = mQuestion.mQuestion.length;
- Random r;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
- r = new Random();
- answer1 = (Button) findViewById(R.id.answer1);
- answer2 = (Button) findViewById(R.id.answer2);
- answer3 = (Button) findViewById(R.id.answer3);
- answer4 = (Button) findViewById(R.id.answer4);
- score = (TextView) findViewById(R.id.score);
- question = (TextView) findViewById(R.id.question);
- score.setText("Score:" + mScore);
- updateQuestion(r.nextInt(mQuestionLength));
- answer1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (answer1.getText() ==mAnswer){
- mScore++;
- score.setText("Score:" + mScore);
- updateQuestion(r.nextInt(mQuestionLength));
- }else {
- gameOver();
- }
- }
- });
- answer2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (answer2.getText() ==mAnswer){
- mScore++;
- score.setText("Score:" + mScore);
- updateQuestion(r.nextInt(mQuestionLength));
- }else {
- gameOver();
- }
- }
- });
- answer3.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (answer3.getText() ==mAnswer){
- mScore++;
- score.setText("Score:" + mScore);
- updateQuestion(r.nextInt(mQuestionLength));
- }else {
- gameOver();
- }
- }
- });
- answer4.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (answer4.getText() ==mAnswer){
- mScore++;
- score.setText("Score:" + mScore);
- updateQuestion(r.nextInt(mQuestionLength));
- }else {
- gameOver();
- }}
- });
- }
- private void updateQuestion(int num){
- question.setText(mQuestion.getQuestion(num));
- answer1.setText(mQuestion.getChoice1(num));
- answer2.setText(mQuestion.getChoice2(num));
- answer3.setText(mQuestion.getChoice3(num));
- answer4.setText(mQuestion.getChoice4(num));
- mAnswer = mQuestion.getCorrectAnswer(num);
- }
- private void gameOver(){
- AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Main2Activity.this);
- alertDialogBuilder
- .setMessage("Game Over! Your Score is" + mScore + "points.")
- .setCancelable(false)
- .setPositiveButton("New Game",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int i) {
- startActivity(new Intent(getApplicationContext(), Main2Activity.class));
- finish();
- }
- })
- .setNegativeButton ("Exit",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int i) {
- finish();
- }
- });
- AlertDialog alertDialog = alertDialogBuilder.create();
- alertDialog.show();
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement