Commit 23de08e3 by likorn

Card buttons instead of radioButtons

parent 84a2e414
...@@ -31,6 +31,7 @@ dependencies { ...@@ -31,6 +31,7 @@ dependencies {
implementation 'androidx.core:core-ktx:1.1.0' implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0' implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test:runner:1.2.0'
......
package com.example.quickmax
class Answer(val buttonId: Int, val value: Int) {
var correct: Boolean = false
}
\ No newline at end of file
package com.example.quickmax
class AnswerSet(numDigits: Int): Iterable<Answer> {
private val buttonIds =
listOf(R.id.btn_left_top, R.id.btn_right_top, R.id.btn_left_bottom, R.id.btn_right_bottom)
private val numOptions = 4
val numbers: MutableList<Answer> =
MutableList(numOptions) { i -> Answer(buttonIds[i], generateRandom(numDigits)) }
init {
val correctAnswer = findSecondMax(numbers.map { n -> n.value })
numbers.forEach { n ->
n.correct = n.value == correctAnswer
}
}
override fun iterator(): Iterator<Answer> {
return numbers.iterator()
}
}
\ No newline at end of file
...@@ -8,7 +8,7 @@ fun generateRandom(numDigits: Int): Int { ...@@ -8,7 +8,7 @@ fun generateRandom(numDigits: Int): Int {
return (start..end).shuffled().first() return (start..end).shuffled().first()
} }
fun findSecondMax(numbers: MutableList<Int>): Int { fun findSecondMax(numbers: List<Int>): Int {
var firstMax = 0 var firstMax = 0
var secondMax = 0 var secondMax = 0
......
...@@ -3,15 +3,15 @@ package com.example.quickmax ...@@ -3,15 +3,15 @@ package com.example.quickmax
import android.os.Bundle import android.os.Bundle
import android.os.CountDownTimer import android.os.CountDownTimer
import android.view.View import android.view.View
import android.widget.Button
import android.widget.RadioButton import android.widget.RadioButton
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.get import androidx.cardview.widget.CardView
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
var numberSet = NumberSet(4, 3) private var answerSet = AnswerSet(4)
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -21,22 +21,19 @@ class MainActivity : AppCompatActivity() { ...@@ -21,22 +21,19 @@ class MainActivity : AppCompatActivity() {
} }
private fun addRadioButtons() { private fun addRadioButtons() {
for (n in numberSet) { for (answer in answerSet) {
val radioButton = RadioButton(this) findViewById<Button>(answer.buttonId).text = answer.value.toString()
radioButton.id = View.generateViewId() findViewById<Button>(answer.buttonId).setOnClickListener { processAnswer(answer.correct) }
radioButton.text = n.toString()
radioButton.setOnClickListener { processAnswer(n) }
radio_group.addView(radioButton)
} }
} }
private fun processAnswer(answer: Int) { private fun processAnswer(correct: Boolean) {
timer.cancel() timer.cancel()
makeRadioButtonsUncheckable() makeRadioButtonsUncheckable()
val responseFragment = ResponseFragment.newInstance().also { val responseFragment = ResponseFragment.newInstance().also {
f -> f.arguments = Bundle().also { f -> f.arguments = Bundle().also {
b -> b.putBoolean("correct", numberSet.isCorrect(answer)) } } b -> b.putBoolean("correct", correct) } }
supportFragmentManager supportFragmentManager
.beginTransaction() .beginTransaction()
...@@ -45,8 +42,8 @@ class MainActivity : AppCompatActivity() { ...@@ -45,8 +42,8 @@ class MainActivity : AppCompatActivity() {
} }
private fun makeRadioButtonsUncheckable() { private fun makeRadioButtonsUncheckable() {
for (i in 0 until radio_group.childCount) { for (answer in answerSet) {
radio_group[i].isClickable = false findViewById<Button>(answer.buttonId).isClickable = false
} }
} }
......
package com.example.quickmax
class NumberSet(numOptions: Int, numDigits: Int): Iterable<Int> {
val numbers: MutableList<Int> = MutableList(numOptions) { generateRandom(numDigits) }
private val correct: Int = findSecondMax(numbers)
override fun iterator(): Iterator<Int> {
return numbers.iterator()
}
fun isCorrect(number: Int) : Boolean {
return number == correct
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFF44336"/>
</shape>
...@@ -5,54 +5,138 @@ ...@@ -5,54 +5,138 @@
android:id="@+id/main_layout" android:id="@+id/main_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#C4BFCC"
tools:context=".MainActivity"> tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/timer_layout" android:id="@+id/layout_answers"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="200dp"
android:layout_marginTop="8dp" android:layout_margin="8dp"
android:layout_marginEnd="8dp" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cv_task"
app:layout_constraintVertical_bias="0.5"
app:layout_constraintVertical_chainStyle="packed">
<ImageView <androidx.cardview.widget.CardView
android:layout_width="wrap_content" android:id="@+id/cv_left_top"
android:layout_height="wrap_content" android:layout_width="0dp"
android:tint="#4CAF50" android:layout_height="0dp"
app:layout_constraintEnd_toStartOf="@+id/tv_time_left" android:layout_margin="8dp"
app:layout_constraintBottom_toTopOf="@+id/cv_left_bottom"
app:layout_constraintEnd_toStartOf="@+id/cv_right_top"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent" >
app:srcCompat="@drawable/ic_timer_small" />
<Button
android:id="@+id/btn_left_top"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFFFF"
android:text="Button" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/cv_right_top"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="8dp"
app:layout_constraintBottom_toTopOf="@id/cv_right_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/cv_left_top"
app:layout_constraintTop_toTopOf="parent" >
<Button
android:id="@+id/btn_right_top"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFFFF"
android:text="Button" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/cv_left_bottom"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cv_right_bottom"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cv_left_top" >
<Button
android:id="@+id/btn_left_bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFFFF"
android:text="Button" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/cv_right_bottom"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/cv_left_bottom"
app:layout_constraintTop_toBottomOf="@+id/cv_right_top" >
<Button
android:id="@+id/btn_right_bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FFFFFF"
android:text="Button" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView <TextView
android:id="@+id/tv_time_left" android:id="@+id/tv_time_left"
android:layout_width="wrap_content" android:layout_width="40dp"
android:layout_height="wrap_content" android:layout_height="40dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="@drawable/circle_button"
android:gravity="center"
android:text="3"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textColor="#FFFFFF"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView <androidx.cardview.widget.CardView
android:id="@+id/task" android:id="@+id/cv_task"
android:layout_width="wrap_content" style="@style/CardView.Light"
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:layout_margin="16dp"
android:text="@string/second_max" app:cardBackgroundColor="#EBF8F8F8"
app:layout_constraintLeft_toLeftOf="parent" app:cardElevation="2dp"
app:layout_constraintBottom_toTopOf="@+id/layout_answers"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:layout_goneMarginStart="8dp" /> app:layout_constraintVertical_bias="0.5"
app:layout_constraintVertical_chainStyle="packed">
<RadioGroup <TextView
android:id="@+id/radio_group" android:id="@+id/tv_task"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp" android:layout_margin="16dp"
android:layout_marginTop="8dp" android:gravity="center"
app:layout_constraintLeft_toLeftOf="parent" android:includeFontPadding="true"
app:layout_constraintTop_toBottomOf="@+id/task" android:text="@string/second_max"
app:layout_goneMarginStart="8dp"> android:textAppearance="@style/TextAppearance.MaterialComponents.Headline5"
android:textColor="#323753" />
</RadioGroup> </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<resources> <resources>
<string name="app_name">QuickMax</string> <string name="app_name">QuickMax</string>
<string name="second_max">Выберите второе наибольшее число</string> <string name="second_max">Choose the second greatest value</string>
<string name="response_correct">CORRECT</string> <string name="response_correct">CORRECT</string>
<string name="response_wrong">WRONG</string> <string name="response_wrong">WRONG</string>
<string name="time_is_over">TIME IS OVER</string> <string name="time_is_over">TIME IS OVER</string>
......
...@@ -4,11 +4,11 @@ import org.junit.Test ...@@ -4,11 +4,11 @@ import org.junit.Test
import org.junit.Assert.* import org.junit.Assert.*
class NumberSetTest { class AnswerSetTest {
private val numOptions = 4 private val numOptions = 4
private val numDigits = 3 private val numDigits = 3
private val numberSet = NumberSet(numOptions, numDigits) private val numberSet = AnswerSet(numOptions, numDigits)
@Test @Test
fun test_constructor() { fun test_constructor() {
......
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