Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.pm.ActivityInfo;
- import android.graphics.Color;
- import android.os.CountDownTimer;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity{
- TextView textViewMAIN;
- TextView textView_N;
- Boolean true_or_false;
- private TextView mTimer;
- private CountDownTimer timer;
- int count = 0;
- int timeMillisSum = 16000;
- int delay = 1000;
- int best_count = 0;
- String[] text = {"Красный", "Синий", "Зелёный", "Чёрный", "Белый", "Жёлтый", "Серый"};
- int[] color = {Color.RED, Color.BLUE, Color.GREEN, Color.BLACK, Color.WHITE, Color.YELLOW, Color.GRAY};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- loadText ();
- textViewMAIN = findViewById(R.id.textViewMAIN);
- textView_N = findViewById(R.id.textView_N);
- mTimer = (TextView) findViewById(R.id.tv);
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
- setTextAndColor();
- timer = new Timer(timeMillisSum, delay);
- timer.start();
- }
- void loadText(){
- SharedPreferences settings = getSharedPreferences ("count", MODE_PRIVATE);
- best_count = settings.getInt ("count", 0);
- }
- void saveText() {
- SharedPreferences sharedPreferences = getSharedPreferences ("count", MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putInt("count", count);
- editor.apply();
- }
- public void setTextAndColor(){
- int a = (int) Math.floor(Math.random() * 2);
- int b = (int) Math.floor(Math.random() * text.length);
- textViewMAIN.setText(text[b]);
- switch (a) {
- case 0:
- textViewMAIN.setTextColor(color[b]);
- true_or_false = true;
- break;
- case 1:
- int c = b;
- while (c == b) {
- c = (int) Math.floor(Math.random() * text.length);
- }
- textViewMAIN.setTextColor(color[c]);
- true_or_false = false;
- break;
- }
- }
- public void buttonTrue(View view){
- if (true_or_false){
- count++;
- textView_N.setText(String.valueOf(count));
- }else{
- onFail();
- }
- setTextAndColor();
- }
- private void onFail() {
- timer.cancel();
- timeMillisSum -= 1000;
- timer = new Timer(timeMillisSum, delay);
- timer.start();
- if(!(count <= 0)) {
- count--;
- }
- textView_N.setText(String.valueOf(count));
- }
- public void buttonFalse(View view){
- if (!true_or_false){
- count++;
- textView_N.setText(String.valueOf(count));
- } else {
- onFail();
- }
- setTextAndColor();
- }
- public class Timer extends CountDownTimer {
- public Timer(long timeMillisSum, long delay) {
- super(timeMillisSum, delay);
- }
- public void onTick(long millisUntilFinished) {
- timeMillisSum -= 1000;
- mTimer.setText("Осталось: "
- + millisUntilFinished / 1000);
- }
- public void onFinish() {
- AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
- builder.setTitle("Время истекло!")
- .setMessage("Ваш счёт:" + count)
- .setCancelable(false)
- .setNegativeButton("Понял",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- if (count > best_count){
- saveText ();
- }
- Intent intent = new Intent(MainActivity.this, Main2Activity.class);
- intent.putExtra("myIntVariableName", count);
- startActivity(intent);
- }
- });
- AlertDialog alert = builder.create();
- alert.show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement