1. package com.test.brzoracunanje;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.text.Editable;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12. import android.os.CountDownTimer;
  13.  
  14. public class BrzoRacunanjeActivity extends Activity implements OnClickListener {
  15.  
  16. TextView broj1, op, broj2, poeni, tvRezultat, tvPreostaloVrijeme1,
  17. tvPreostaloVrijeme;
  18. Button start, posalji;
  19. int brojevi[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 25, 50, 100 };
  20. String operatori[] = { "+", "-", "/", "*" };
  21. EditText et1;
  22. int poenibrojanje = 0;
  23. int rez = 0;
  24.  
  25. public void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.main);
  28.  
  29. broj1 = (TextView) findViewById(R.id.textView1);
  30. op = (TextView) findViewById(R.id.textView2);
  31. broj2 = (TextView) findViewById(R.id.textView3);
  32. poeni = (TextView) findViewById(R.id.textView7);
  33. tvRezultat = (TextView) findViewById(R.id.textView4);
  34. tvPreostaloVrijeme1 = (TextView) findViewById(R.id.textView5);
  35. tvPreostaloVrijeme = (TextView) findViewById(R.id.textView6);
  36. start = (Button) findViewById(R.id.button2);
  37. posalji = (Button) findViewById(R.id.button1);
  38.  
  39. start.setOnClickListener(this);
  40. posalji.setOnClickListener(this);
  41. }
  42.  
  43. public void racunanje() {
  44. int a = Integer.parseInt((String) broj1.getText());
  45. int b = Integer.parseInt((String) broj2.getText());
  46.  
  47. String c = op.getText().toString();
  48.  
  49. if (c.equals("+")) {
  50.  
  51. rez = a + b;
  52. } else if (c.equals("-"))
  53. rez = a - b;
  54. else if (c.equals("*"))
  55. rez = a * b;
  56. else if (c.equals("/"))
  57. rez = a / b;
  58.  
  59. tvPreostaloVrijeme1.setText(Integer.toString(rez));
  60.  
  61. }
  62.  
  63. public void posaljiOdgovor() {
  64.  
  65. //int x =Integer.parseInt(et1.getText().toString());
  66.  
  67. String xS = et1.getText().toString();
  68. int x = Integer.parseInt(xS);
  69.  
  70.  
  71. if (x == rez) {
  72. tvPreostaloVrijeme.setText("Tacan odgovor +1!");
  73. poenibrojanje++;
  74. poeni.setText(poenibrojanje);
  75.  
  76. } else
  77. tvPreostaloVrijeme.setText("Pogresan odgovor");
  78. }
  79.  
  80. @Override
  81. public void onClick(View v) {
  82.  
  83. // fali STOP dugme i klasa
  84.  
  85. switch (v.getId()) {
  86.  
  87. case R.id.button2:
  88. broj1.setText(Integer.toString(brojevi()));
  89. broj2.setText(Integer.toString(brojevi()));
  90. op.setText(operatori());
  91. racunanje();
  92.  
  93. new CountDownTimer(30000, 1000) {
  94.  
  95. public void onTick(long millisUntilFinished) {
  96. tvPreostaloVrijeme.setText(Integer
  97. .toString((int) (millisUntilFinished / 1000)));
  98. }
  99.  
  100. public void onFinish() {
  101. tvPreostaloVrijeme.setText("Isteklo vrijeme...");
  102. broj1.setText("");
  103. broj2.setText("");
  104. op.setText("");
  105.  
  106. }
  107. }.start();
  108.  
  109. break;
  110.  
  111. case R.id.button1:
  112.  
  113. posaljiOdgovor();
  114.  
  115. }
  116.  
  117. }
  118.  
  119. public int brojevi() {
  120. int i;
  121. i = (int) (Math.random() * brojevi.length);
  122. return brojevi[i];
  123. }
  124.  
  125. public String operatori() {
  126. int i;
  127. i = (int) (Math.random() * operatori.length);
  128. return operatori[i];
  129. }
  130. }