Android Studio Kotlin Show下一个TextView Integer

发布于 2025-01-27 06:50:34 字数 522 浏览 1 评论 0原文

我试图创建一个发电机,当我单击“启动”按钮时,文本视图给我AA随机数。 这很好, 但是我想显示数字,我仍然点击停止 因此,它必须从我的可变列表中给我每几个secons一个新的随机数。 我该怎么做? 我的第一个意见是递归功能。

private fun run() {
    var x = 0
    var randomListe = mutableListOf<Int>()
    randomListe.add(Random.nextInt())
    x++
    for (element in randomListe) {
        var x = 0
        val zahlInListe = randomListe[x]
        //     Thread.sleep(1_000)
        // I tried while here
        textView.text = ("${randomListe[x]} \n das ist die Random Zahl moruk\n")

    }
}

Im trying to create a generator, when i click the start button, the textview gives me a a random number.
This works great,
but i want to show the numbers still i click stop
so it must give me every few secons a new random number from my mutable List.
How could I do this?
My first opinion was, recursive function.

private fun run() {
    var x = 0
    var randomListe = mutableListOf<Int>()
    randomListe.add(Random.nextInt())
    x++
    for (element in randomListe) {
        var x = 0
        val zahlInListe = randomListe[x]
        //     Thread.sleep(1_000)
        // I tried while here
        textView.text = ("${randomListe[x]} \n das ist die Random Zahl moruk\n")

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

围归者 2025-02-03 06:50:34

您可以 a coroutine lifecyclescope为此,您也可以删除冗余x用于保持当前<代码>索引要显示最后一个值,您可以使用 arraylist的方法>在<<中获取last value in <<代码>列表

运行方法进行更改,然后返回job对象,因此您可以在停止的用户点击时稍后取消它按钮。

private fun run(): Job {
    return lifecycleScope.launch {
        while(true){
            delay(1000)

            var randomListe = mutableListOf<Int>()
            randomListe.add(Random.nextInt())
            textView.text = ("${randomListe.last()} \n das ist die Random Zahl moruk\n")
        }
    }
}

现在,将返回job值保持在变量中,呼叫运行方法

private var job: Job? = null
 
job = run() 

调用 cance> cance on job用户停止按钮上的水龙头

btnStop.setOnClickListener{
    job?.cancel()
}

You can launch a coroutine block in lifecycleScope for this and you can also remove redundant x for keeping the current index to show the last value, you can use the last method of ArrayList to get the last value in the list

Make changes to your run method and return a Job object from it so you can cancel it later when the user taps on the stop button.

private fun run(): Job {
    return lifecycleScope.launch {
        while(true){
            delay(1000)

            var randomListe = mutableListOf<Int>()
            randomListe.add(Random.nextInt())
            textView.text = ("${randomListe.last()} \n das ist die Random Zahl moruk\n")
        }
    }
}

Now keep the return job value in a variable, on calling the run method

private var job: Job? = null
 
job = run() 

Call cancel on job when the user taps on the stop button

btnStop.setOnClickListener{
    job?.cancel()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文