Android Studio Kotlin Show下一个TextView Integer
我试图创建一个发电机,当我单击“启动”按钮时,文本视图给我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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以
a
coroutine
lifecyclescope
为此,您也可以删除冗余x
用于保持当前<代码>索引要显示最后一个值,您可以使用arraylist
的方法>在<<中获取last
value in <<代码>列表对
运行
方法进行更改,然后返回job
对象,因此您可以在停止的用户点击时稍后取消它
按钮。现在,将返回
job
值保持在变量中,呼叫运行
方法调用 cance> cance on
job
用户停止按钮上的水龙头You can
launch
acoroutine
block inlifecycleScope
for this and you can also remove redundantx
for keeping the currentindex
to show the last value, you can use thelast
method ofArrayList
to get thelast
value in thelist
Make changes to your
run
method and return aJob
object from it so you can cancel it later when the user taps on thestop
button.Now keep the return
job
value in a variable, on calling therun
methodCall
cancel
onjob
when the user taps on the stop button