How to call a function after delay in Kotlin?

前端 未结 11 1930
南笙
南笙 2020-11-30 19:07

As the title, is there any way to call a function after delay (1 second for example) in Kotlin?

11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 19:58

    A simple example to show a toast after 3 seconds :

    fun onBtnClick() {
        val handler = Handler()
        handler.postDelayed({ showToast() }, 3000)
    }
    
    fun showToast(){
        Toast.makeText(context, "Its toast!", Toast.LENGTH_SHORT).show()
    }
    

提交回复
热议问题