|
| 1 | +package com.komdosh.pral.Display; |
| 2 | + |
| 3 | +import android.animation.Animator; |
| 4 | +import android.animation.AnimatorListenerAdapter; |
| 5 | +import android.content.Intent; |
| 6 | +import android.graphics.Typeface; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.os.SystemClock; |
| 9 | +import android.support.design.widget.TextInputLayout; |
| 10 | +import android.support.v7.app.AppCompatActivity; |
| 11 | +import android.util.TypedValue; |
| 12 | +import android.view.KeyEvent; |
| 13 | +import android.view.View; |
| 14 | +import android.view.ViewGroup; |
| 15 | +import android.view.inputmethod.EditorInfo; |
| 16 | +import android.widget.Button; |
| 17 | +import android.widget.Chronometer; |
| 18 | +import android.widget.EditText; |
| 19 | +import android.widget.RelativeLayout; |
| 20 | +import android.widget.TextView; |
| 21 | + |
| 22 | +import com.komdosh.pral.R; |
| 23 | +import com.komdosh.pral.System.Math.MathTask; |
| 24 | +import com.komdosh.pral.System.Math.Mathematics; |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * Created by Komdosh on 24.09.2016. |
| 29 | + */ |
| 30 | + |
| 31 | +public class MathematicsDisplay extends AppCompatActivity { |
| 32 | + EditText et; |
| 33 | + final Mathematics m = new Mathematics(); |
| 34 | + final MathTask t = m.generateTask(); |
| 35 | + @Override |
| 36 | + protected void onCreate(Bundle savedInstanceState) { |
| 37 | + super.onCreate(savedInstanceState); |
| 38 | + setContentView(R.layout.math_layout); |
| 39 | + et = (EditText) findViewById(R.id.editText); |
| 40 | + TextInputLayout v = (TextInputLayout)findViewById(R.id.txt); |
| 41 | + v.setHint(getString(R.string.example, t.task[0],t.task[1],t.task[2])); |
| 42 | + v.setHintTextAppearance(R.style.InputLayout); |
| 43 | + |
| 44 | + et.setOnKeyListener(new View.OnKeyListener() { |
| 45 | + @Override |
| 46 | + public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 47 | + if(check()) |
| 48 | + rightAnswer(); |
| 49 | + return false; |
| 50 | + } |
| 51 | + }); |
| 52 | + et.setOnEditorActionListener(new EditText.OnEditorActionListener() { |
| 53 | + @Override |
| 54 | + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 55 | + if(actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.KEYCODE_ENTER) |
| 56 | + rightAnswer(); |
| 57 | + return false; |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + private void startChronometr(){ |
| 63 | + final Chronometer mChronometer = (Chronometer) findViewById(R.id.chronometer); |
| 64 | + mChronometer.setBase(SystemClock.elapsedRealtime()); |
| 65 | + mChronometer.start(); |
| 66 | + mChronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { |
| 67 | + @Override |
| 68 | + public void onChronometerTick(Chronometer chronometer) { |
| 69 | + long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase(); |
| 70 | + |
| 71 | + if(elapsedMillis>=60*1000){ |
| 72 | + nextActivity(); |
| 73 | + } |
| 74 | + |
| 75 | +/* if (elapsedMillis > 5000) { |
| 76 | + String strElapsedMillis = "Прошло больше 5 секунд"; |
| 77 | + Toast.makeText(getApplicationContext(), |
| 78 | + strElapsedMillis, Toast.LENGTH_SHORT) |
| 79 | + .show(); |
| 80 | + }*/ |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + private void nextActivity(){ |
| 86 | + Intent i = new Intent(this, WordDisplay.class); |
| 87 | + finish(); |
| 88 | + startActivity(i); |
| 89 | + } |
| 90 | + |
| 91 | + private boolean check(){ |
| 92 | + return et.getText().toString().equals(t.answer.toString()); |
| 93 | + } |
| 94 | + |
| 95 | + private void rightAnswer(){ |
| 96 | + rightPopup(); |
| 97 | + t.set(m.generateTask()); |
| 98 | + et.setText(""); |
| 99 | + TextInputLayout v = (TextInputLayout)findViewById(R.id.txt); |
| 100 | + v.setHint(getString(R.string.example, t.task[0],t.task[1],t.task[2])); |
| 101 | + TextView counter = (TextView)findViewById(R.id.counterRight); |
| 102 | + counter.setText(""+(Integer.parseInt(counter.getText().toString())+1)); |
| 103 | + } |
| 104 | + |
| 105 | + public void start(View view){ |
| 106 | + final RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl); |
| 107 | + final Button b = (Button)findViewById(R.id.button); |
| 108 | + |
| 109 | + startChronometr(); |
| 110 | + |
| 111 | + rl.animate() |
| 112 | + .alpha(0.0f) |
| 113 | + .setDuration(600) |
| 114 | + .setListener(new AnimatorListenerAdapter() { |
| 115 | + @Override |
| 116 | + public void onAnimationEnd(Animator animation) { |
| 117 | + super.onAnimationEnd(animation); |
| 118 | + rl.setVisibility(View.INVISIBLE); |
| 119 | + } |
| 120 | + }); |
| 121 | + b.animate() |
| 122 | + .alpha(0.0f) |
| 123 | + .setDuration(500) |
| 124 | + .setListener(new AnimatorListenerAdapter() { |
| 125 | + @Override |
| 126 | + public void onAnimationEnd(Animator animation) { |
| 127 | + super.onAnimationEnd(animation); |
| 128 | + b.setVisibility(View.INVISIBLE); |
| 129 | + } |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + protected void rightPopup(){ |
| 134 | + TextView rightTextView = new TextView(this); |
| 135 | + RelativeLayout.LayoutParams textViewLayoutParams = |
| 136 | + new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| 137 | + textViewLayoutParams.setMargins(0, 150, 0, 0); // setMargins(left, top, right, bottom); |
| 138 | + textViewLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL); |
| 139 | + rightTextView.setLayoutParams(textViewLayoutParams); |
| 140 | + |
| 141 | + rightTextView.setId(R.id.rightTextView); |
| 142 | + rightTextView.setTextColor(0xFF22F74F); |
| 143 | + rightTextView.setTypeface(rightTextView.getTypeface(), Typeface.BOLD); |
| 144 | + rightTextView.setText("Right!"); |
| 145 | + rightTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP,20); |
| 146 | + rightTextView.animate() |
| 147 | + .translationY(-50) |
| 148 | + .alpha(0.0f) |
| 149 | + .setDuration(1500); |
| 150 | + RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.activity_main); |
| 151 | + relativeLayout.addView(rightTextView); |
| 152 | + } |
| 153 | +} |
0 commit comments