Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.mathapplication;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.TextView;
- import androidx.appcompat.app.AppCompatActivity;
- public class PlayActivity extends AppCompatActivity {
- TextView tvNo1, tvOp, tvNo2, tvScore;
- EditText etAnswer;
- ImageView imgImage;
- Exercise ex;
- int countAll = 0, countRight = 0;
- Handler handler = new Handler();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_play);
- tvNo1 = findViewById(R.id.tvNo1);
- tvNo2 = findViewById(R.id.tvNo2);
- tvOp = findViewById(R.id.tvOp);
- etAnswer = findViewById(R.id.etAnswer);
- imgImage = findViewById(R.id.imgImage);
- tvScore = findViewById(R.id.tvScore);
- showEx();
- }
- private void showEx() {
- ex = new Exercise();
- tvNo1.setText(ex.getN1() + "");
- tvOp.setText(ex.getOp());
- tvNo2.setText(ex.getN2() + "");
- etAnswer.setText("");
- imgImage.setVisibility(View.INVISIBLE);
- }
- public void checkAnswer(View view) {
- imgImage.setVisibility(View.VISIBLE);
- int answer = Integer.parseInt(etAnswer.getText().toString());
- if (answer == ex.getValue()) {
- // right answer
- imgImage.setImageResource(R.drawable.right);
- countAll++;
- countRight++;
- tvScore.setText(countRight + " /" + countAll);
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- showEx();
- }
- }, 3000);
- } else {
- // wrong answer
- imgImage.setImageResource((R.drawable.error));
- countAll++;
- tvScore.setText(countRight + " /" + countAll);
- }
- }
- }
Add Comment
Please, Sign In to add comment