简体   繁体   中英

I cant get text to change on my android application on android studio

In my code I have a letter appear in Russian, I then write the sound it is in English. Press a submit button and it checks if I am correct, shows correct or incorrect based on if I was right then shows the next letter. My code will show a new random letter each time I go to the page, however when I press the submit button it doesn't change the text to say weather I was right or not. I used the debug menu to find that it does check if my answer was correct and can detect if it is right, however it doesn't change anything visually

package com.example.russianpractice

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.SystemClock.sleep
import android.widget.Button
import android.widget.EditText
import android.widget.TextView

class Alphabet : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_alphabet)

        val rnd = ((-1..34).random()).toInt();
        val button = findViewById<Button>(R.id.back)
        val buttonA = findViewById<Button>(R.id.answer)
        val answer = findViewById<EditText>(R.id.answer_letter)
        val text = findViewById<TextView>(R.id.random_Letter)
        val rLetter = arrayOf("А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж", "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ы", "Ь", "Э", "Ю", "Я")
        val aSound = arrayOf("a", "b", "v", "g", "d", "ye", "yo", "zh", "z", "i", "ey", "k", "l", "m", "n", "o", "p", "r", "s", "t", "oo", "f", "kh", "ts", "ch", "sh", "shch", "hard", "y", "soft", "e", "yu", "ya")
        var num = rnd
        var check = ""
        text.text = rLetter[num]


        buttonA.setOnClickListener(){
            check = answer.text.toString()
            if (check == aSound[num]) {
                text.text= "Correct"
            } else {
                text.text= "Wrong"
            }
            sleep(1000)
            num = rnd
            text.text = rLetter[num]
        }

        button.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }




    }
}

You want to display the result "Correct" or "Wrong" in the TextView ( random_Letter ), which you also use to display the random letter.
To do so you set the TextView 's text to the result and then you use sleep(1000) which makes the interface freeze for 1 second and then you display in the same TextView the same random letter as the previous one.
So finally you don't see the result that you want.

What I suggest is use another TextView or a Toast to display "Correct" or "Wrong" :

val button = findViewById<Button>(R.id.back)
val buttonA = findViewById<Button>(R.id.answer)
val answer = findViewById<EditText>(R.id.answer_letter)
val text = findViewById<TextView>(R.id.random_Letter)
val rLetter = arrayOf("А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж", "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ы", "Ь", "Э", "Ю", "Я")
val aSound = arrayOf("a", "b", "v", "g", "d", "ye", "yo", "zh", "z", "i", "ey", "k", "l", "m", "n", "o", "p", "r", "s", "t", "oo", "f", "kh", "ts", "ch", "sh", "shch", "hard", "y", "soft", "e", "yu", "ya")

var num = (0 until rLetter.size).random()
text.text = rLetter[num]

buttonA.setOnClickListener(){
    Toast.makeText(this, if (answer.text.toString() == aSound[num]) "Correct" else "Wrong", Toast.LENGTH_LONG).show()
    num = (0 until rLetter.size).random()
    text.text = rLetter[num]
} 

I also changed ((-1..34).random()).toInt() to (0 until rLetter.size).random() for the creation of the random integer, because -1 does not make sense as you want that random integer to be the index of the array and instead of hardcoding the array's size you can use rLetter.size .

Also, inside the listener, after you display the result, you must create each time a new random number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM