Advertisement
ZRE0412

challenge99_main

Sep 8th, 2021
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.01 KB | None | 0 0
  1. package com.example.proj_99
  2.  
  3. import android.graphics.drawable.Drawable
  4. import android.media.Image
  5. import androidx.appcompat.app.AppCompatActivity
  6. import android.os.Bundle
  7. import android.widget.*
  8. import kotlin.random.Random
  9.  
  10. class MainActivity : AppCompatActivity() {
  11.     override fun onCreate(savedInstanceState: Bundle?) {
  12.         // init
  13.         super.onCreate(savedInstanceState)
  14.         setContentView(R.layout.activity_main)
  15.  
  16.         // Code this
  17.         val calc = findViewById<Button>(R.id.calc)
  18.         val retry = findViewById<Button>(R.id.retry)
  19.         val multiplicand = findViewById<TextView>(R.id.multiplicand)
  20.         val multiplier = findViewById<TextView>(R.id.multiplier)
  21.         val result = findViewById<TextView>(R.id.result)
  22.         val input = findViewById<EditText>(R.id.input)
  23.         val image = findViewById<ImageView>(R.id.image)
  24.  
  25.         getNewQuestion(multiplicand, multiplier)
  26.  
  27.         calc.setOnClickListener {
  28.             if (input.text.toString() != "" && input.text.toString()
  29.                     .toInt() == multiplicand.text.toString().toInt() * multiplier.text.toString()
  30.                     .toInt()
  31.             ) {
  32.                 result.text = "恭喜你答對了"
  33.                 cleanTextChangeImage(image, input, "yes")
  34.             } else {
  35.                 result.text = "答錯了,再算一次"
  36.                 cleanTextChangeImage(image, input, "no")
  37.             }
  38.         }
  39.  
  40.         retry.setOnClickListener {
  41.             getNewQuestion(multiplicand, multiplier)
  42.             cleanTextChangeImage(image, input,"qa")
  43.             result.text = "?"
  44.         }
  45.  
  46.     }
  47.  
  48.     fun getNewQuestion(multiplicand :TextView, multiplier :TextView) {
  49.         multiplicand.text = Random.nextInt(1, 10).toString()
  50.         multiplier.text = Random.nextInt(1, 10).toString()
  51.     }
  52.  
  53.     fun cleanTextChangeImage(image :ImageView, input :EditText,id :String) {
  54.         image.setImageResource(resources.getIdentifier(id, "drawable", packageName))
  55.         input.text.clear()
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement