Commit 4a6665cc by likorn

Timer is stopped after answering

parent b367ef90
...@@ -17,23 +17,7 @@ class MainActivity : AppCompatActivity() { ...@@ -17,23 +17,7 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
addRadioButtons() addRadioButtons()
setTimer() timer.start()
}
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()
} }
private fun addRadioButtons() { private fun addRadioButtons() {
...@@ -47,6 +31,9 @@ class MainActivity : AppCompatActivity() { ...@@ -47,6 +31,9 @@ class MainActivity : AppCompatActivity() {
} }
private fun processAnswer(answer: Int) { private fun processAnswer(answer: Int) {
timer.cancel()
makeRadioButtonsUncheckable()
val responseFragment: Fragment = if (numberSet.isCorrect(answer)) { val responseFragment: Fragment = if (numberSet.isCorrect(answer)) {
ResponseCorrectFragment.newInstance() ResponseCorrectFragment.newInstance()
} else } else
...@@ -56,7 +43,6 @@ class MainActivity : AppCompatActivity() { ...@@ -56,7 +43,6 @@ class MainActivity : AppCompatActivity() {
.beginTransaction() .beginTransaction()
.add(R.id.main_layout, responseFragment, "response") .add(R.id.main_layout, responseFragment, "response")
.commitAllowingStateLoss() .commitAllowingStateLoss()
makeRadioButtonsUncheckable()
} }
private fun makeRadioButtonsUncheckable() { private fun makeRadioButtonsUncheckable() {
...@@ -64,4 +50,18 @@ class MainActivity : AppCompatActivity() { ...@@ -64,4 +50,18 @@ class MainActivity : AppCompatActivity() {
radio_group[i].isClickable = false 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 ...@@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageButton
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
class ResponseCorrectFragment: Fragment() { class ResponseCorrectFragment: Fragment() {
...@@ -17,6 +18,14 @@ class ResponseCorrectFragment: Fragment() { ...@@ -17,6 +18,14 @@ class ResponseCorrectFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater, override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle?): View? { 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 @@ ...@@ -7,11 +7,10 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
android:background="#4CAF50" android:background="#4CAF50"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:tools="http://schemas.android.com/tools">
<ImageButton <ImageButton
android:id="@+id/imageButton" android:id="@+id/btn_next"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
app:layout_constraintStart_toStartOf="parent"> app:layout_constraintStart_toStartOf="parent">
<ImageButton <ImageButton
android:id="@+id/btn_next"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" 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