How do you display a Toast using Kotlin on Android?

前端 未结 15 2212
遇见更好的自我
遇见更好的自我 2021-01-30 16:08

In different Kotlin examples for Android I see toast("Some message...") or toastLong("Some long message"). For example:



        
15条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 16:20

    Try this

    In Activity

    Toast.makeText(applicationContext, "Test", Toast.LENGTH_LONG).show()
    

    or

    Toast.makeText(this@MainActiivty, "Test", Toast.LENGTH_LONG).show()
    

    In Fragment

    Toast.makeText(activity, "Test", Toast.LENGTH_LONG).show()
    

    or

    Toast.makeText(activity?.applicationContext, "Test", Toast.LENGTH_LONG).show()
    

提交回复
热议问题