Commit 4a6665cc by likorn

Timer is stopped after answering

parent b367ef90
......@@ -17,23 +17,7 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
addRadioButtons()
setTimer()
}
private fun setTimer() {
object : CountDownTimer(4000, 1000) {
override fun onTick(millisUntilFinished: Long) {
tv_time_left.text = (millisUntilFinished / 1000).toString()
}
override fun onFinish() {
makeRadioButtonsUncheckable()
supportFragmentManager
.beginTransaction()
.add(R.id.main_layout, TimeIsOverFragment.newInstance(), "time_is_over")
.commitAllowingStateLoss()
}
}.start()
timer.start()
}
private fun addRadioButtons() {
......@@ -47,6 +31,9 @@ class MainActivity : AppCompatActivity() {
}
private fun processAnswer(answer: Int) {
timer.cancel()
makeRadioButtonsUncheckable()
val responseFragment: Fragment = if (numberSet.isCorrect(answer)) {
ResponseCorrectFragment.newInstance()
} else
......@@ -56,7 +43,6 @@ class MainActivity : AppCompatActivity() {
.beginTransaction()
.add(R.id.main_layout, responseFragment, "response")
.commitAllowingStateLoss()
makeRadioButtonsUncheckable()
}
private fun makeRadioButtonsUncheckable() {
......@@ -64,4 +50,18 @@ class MainActivity : AppCompatActivity() {
radio_group[i].isClickable = false
}
}
private val timer = object : CountDownTimer(4000, 1000) {
override fun onTick(millisUntilFinished: Long) {
tv_time_left.text = (millisUntilFinished / 1000).toString()
}
override fun onFinish() {
makeRadioButtonsUncheckable()
supportFragmentManager
.beginTransaction()
.add(R.id.main_layout, TimeIsOverFragment.newInstance(), "time_is_over")
.commitAllowingStateLoss()
}
}
}
......@@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import androidx.fragment.app.Fragment
class ResponseCorrectFragment: Fragment() {
......@@ -17,6 +18,14 @@ class ResponseCorrectFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_response_correct, container, false)
val view = inflater.inflate(R.layout.fragment_response_correct, container, false)
view.findViewById<ImageButton>(R.id.btn_next).setOnClickListener { reloadActivity() }
return view
}
private fun reloadActivity() {
val intent = activity?.intent
activity?.finish()
activity?.startActivity(intent)
}
}
\ No newline at end of file
......@@ -7,11 +7,10 @@
app:layout_constraintStart_toStartOf="parent"
android:background="#4CAF50"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageButton
android:id="@+id/imageButton"
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
......
......@@ -10,6 +10,7 @@
app:layout_constraintStart_toStartOf="parent">
<ImageButton
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment