Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.proj_99
- import android.graphics.drawable.Drawable
- import android.media.Image
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.widget.*
- import kotlin.random.Random
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- // init
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- // Code this
- val calc = findViewById<Button>(R.id.calc)
- val retry = findViewById<Button>(R.id.retry)
- val multiplicand = findViewById<TextView>(R.id.multiplicand)
- val multiplier = findViewById<TextView>(R.id.multiplier)
- val result = findViewById<TextView>(R.id.result)
- val input = findViewById<EditText>(R.id.input)
- val image = findViewById<ImageView>(R.id.image)
- getNewQuestion(multiplicand, multiplier)
- calc.setOnClickListener {
- if (input.text.toString() != "" && input.text.toString()
- .toInt() == multiplicand.text.toString().toInt() * multiplier.text.toString()
- .toInt()
- ) {
- result.text = "恭喜你答對了"
- cleanTextChangeImage(image, input, "yes")
- } else {
- result.text = "答錯了,再算一次"
- cleanTextChangeImage(image, input, "no")
- }
- }
- retry.setOnClickListener {
- getNewQuestion(multiplicand, multiplier)
- cleanTextChangeImage(image, input,"qa")
- result.text = "?"
- }
- }
- fun getNewQuestion(multiplicand :TextView, multiplier :TextView) {
- multiplicand.text = Random.nextInt(1, 10).toString()
- multiplier.text = Random.nextInt(1, 10).toString()
- }
- fun cleanTextChangeImage(image :ImageView, input :EditText,id :String) {
- image.setImageResource(resources.getIdentifier(id, "drawable", packageName))
- input.text.clear()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement