class MainActivity : AppCompatActivity() {
private var s:Int = 0
private var running: Boolean = false
fun onClickStart(view: View) {
running = true
}
fun onClickStop(view: View) {
running = false
}
fun onClickReset(view: View) {
running = false
s = 0
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
runTimer()
}
private fun runTimer() {
val time_view = findViewById<TextView>(R.id.time_view)
val handler: Handler = Handler()
handler.post(
Runnable {
@Override
fun run() {
var hours:Int = s/3600
var minutes: Int = (s%3600)/60
var secs:Int = s%60
var time:String = String.format(Locale.getDefault(), "%d:%02d:%02d", hours, minutes, secs)
time_view.setText(time)
if(running) {
s++
}
handler.postDelayed(this,1000)
}
}
)
}
}
Runnable {
@Override
fun run() {
object: Runnable {
override fun run() {
handler.postDelayed(this)
}
}
Runnable {
//тут код, который должен быть в run()
}