Advertisement
Guest User

Untitled

a guest
May 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. import android.content.DialogInterface;
  2. import android.content.Intent;
  3. import android.content.SharedPreferences;
  4. import android.content.pm.ActivityInfo;
  5. import android.graphics.Color;
  6. import android.os.CountDownTimer;
  7. import android.support.v7.app.AlertDialog;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.TextView;
  12.  
  13. public class MainActivity extends AppCompatActivity{
  14.  
  15. TextView textViewMAIN;
  16. TextView textView_N;
  17. Boolean true_or_false;
  18. private TextView mTimer;
  19. private CountDownTimer timer;
  20. int count = 0;
  21. int timeMillisSum = 16000;
  22. int delay = 1000;
  23. int best_count = 0;
  24. String[] text = {"Красный", "Синий", "Зелёный", "Чёрный", "Белый", "Жёлтый", "Серый"};
  25. int[] color = {Color.RED, Color.BLUE, Color.GREEN, Color.BLACK, Color.WHITE, Color.YELLOW, Color.GRAY};
  26.  
  27.  
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_main);
  32. loadText ();
  33.  
  34. textViewMAIN = findViewById(R.id.textViewMAIN);
  35. textView_N = findViewById(R.id.textView_N);
  36. mTimer = (TextView) findViewById(R.id.tv);
  37. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  38.  
  39. setTextAndColor();
  40.  
  41.  
  42. timer = new Timer(timeMillisSum, delay);
  43. timer.start();
  44. }
  45.  
  46. void loadText(){
  47. SharedPreferences settings = getSharedPreferences ("count", MODE_PRIVATE);
  48. best_count = settings.getInt ("count", 0);
  49. }
  50.  
  51. void saveText() {
  52. SharedPreferences sharedPreferences = getSharedPreferences ("count", MODE_PRIVATE);
  53. SharedPreferences.Editor editor = sharedPreferences.edit();
  54. editor.putInt("count", count);
  55. editor.apply();
  56. }
  57.  
  58. public void setTextAndColor(){
  59. int a = (int) Math.floor(Math.random() * 2);
  60.  
  61. int b = (int) Math.floor(Math.random() * text.length);
  62.  
  63.  
  64. textViewMAIN.setText(text[b]);
  65.  
  66. switch (a) {
  67. case 0:
  68.  
  69. textViewMAIN.setTextColor(color[b]);
  70. true_or_false = true;
  71. break;
  72. case 1:
  73. int c = b;
  74. while (c == b) {
  75. c = (int) Math.floor(Math.random() * text.length);
  76. }
  77. textViewMAIN.setTextColor(color[c]);
  78. true_or_false = false;
  79. break;
  80.  
  81. }
  82. }
  83.  
  84. public void buttonTrue(View view){
  85. if (true_or_false){
  86. count++;
  87. textView_N.setText(String.valueOf(count));
  88. }else{
  89. onFail();
  90. }
  91. setTextAndColor();
  92. }
  93.  
  94. private void onFail() {
  95. timer.cancel();
  96. timeMillisSum -= 1000;
  97. timer = new Timer(timeMillisSum, delay);
  98. timer.start();
  99. if(!(count <= 0)) {
  100. count--;
  101. }
  102.  
  103. textView_N.setText(String.valueOf(count));
  104. }
  105.  
  106. public void buttonFalse(View view){
  107. if (!true_or_false){
  108. count++;
  109. textView_N.setText(String.valueOf(count));
  110. } else {
  111. onFail();
  112. }
  113. setTextAndColor();
  114. }
  115.  
  116. public class Timer extends CountDownTimer {
  117. public Timer(long timeMillisSum, long delay) {
  118. super(timeMillisSum, delay);
  119. }
  120.  
  121. public void onTick(long millisUntilFinished) {
  122. timeMillisSum -= 1000;
  123. mTimer.setText("Осталось: "
  124. + millisUntilFinished / 1000);
  125. }
  126. public void onFinish() {
  127.  
  128. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  129. builder.setTitle("Время истекло!")
  130. .setMessage("Ваш счёт:" + count)
  131. .setCancelable(false)
  132. .setNegativeButton("Понял",
  133. new DialogInterface.OnClickListener() {
  134. public void onClick(DialogInterface dialog, int id) {
  135. if (count > best_count){
  136. saveText ();
  137. }
  138. Intent intent = new Intent(MainActivity.this, Main2Activity.class);
  139. intent.putExtra("myIntVariableName", count);
  140.  
  141. startActivity(intent);
  142. }
  143. });
  144. AlertDialog alert = builder.create();
  145. alert.show();
  146. }
  147. }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement