mmayoub

MathApp, PlayActivity.java

Sep 11th, 2021 (edited)
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. package com.example.mathapplication;
  2.  
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.view.View;
  6. import android.widget.EditText;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9.  
  10. import androidx.appcompat.app.AppCompatActivity;
  11.  
  12. public class PlayActivity extends AppCompatActivity {
  13.     TextView tvNo1, tvOp, tvNo2, tvScore;
  14.     EditText etAnswer;
  15.     ImageView imgImage;
  16.     Exercise ex;
  17.  
  18.     int countAll = 0, countRight = 0;
  19.  
  20.     Handler handler = new Handler();
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_play);
  26.  
  27.         tvNo1 = findViewById(R.id.tvNo1);
  28.         tvNo2 = findViewById(R.id.tvNo2);
  29.         tvOp = findViewById(R.id.tvOp);
  30.         etAnswer = findViewById(R.id.etAnswer);
  31.         imgImage = findViewById(R.id.imgImage);
  32.         tvScore = findViewById(R.id.tvScore);
  33.         showEx();
  34.     }
  35.  
  36.     private void showEx() {
  37.         ex = new Exercise();
  38.         tvNo1.setText(ex.getN1() + "");
  39.         tvOp.setText(ex.getOp());
  40.         tvNo2.setText(ex.getN2() + "");
  41.         etAnswer.setText("");
  42.         imgImage.setVisibility(View.INVISIBLE);
  43.     }
  44.  
  45.     public void checkAnswer(View view) {
  46.         imgImage.setVisibility(View.VISIBLE);
  47.         int answer = Integer.parseInt(etAnswer.getText().toString());
  48.         if (answer == ex.getValue()) {
  49.             // right answer
  50.             imgImage.setImageResource(R.drawable.right);
  51.             countAll++;
  52.             countRight++;
  53.             tvScore.setText(countRight + " /" + countAll);
  54.  
  55.  
  56.             handler.postDelayed(new Runnable() {
  57.                 @Override
  58.                 public void run() {
  59.                     showEx();
  60.                 }
  61.             }, 3000);
  62.  
  63.         } else {
  64.             // wrong answer
  65.             imgImage.setImageResource((R.drawable.error));
  66.             countAll++;
  67.             tvScore.setText(countRight + " /" + countAll);
  68.         }
  69.  
  70.  
  71.     }
  72. }
Add Comment
Please, Sign In to add comment