如何在Kotlin中访问“Activity.this”?

[英]How to access “Activity.this” in Kotlin?


I have this piece of Java code:

我有这段Java代码:

MaterialDialog builder = new MaterialDialog.Builder(MainActivity.this)

I want to get the MainActivity object in Kotlin. The automatic conversion breaks at MainActivity.this.

我想在Kotlin中获取MainActivity对象。 MainActivity.this上的自动转换中断。

1 个解决方案

#1


80  

You can get a reference to your MainActivity object in Kotlin by using a qualified this. e.g.:

您可以使用限定条件在Kotlin中获取对MainActivity对象的引用。例如。:

class MyActivity : MainActivity() {
    val builder = MaterialDialog.Builder(this@MyActivity)
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2017/01/12/d47198ceec790c1678cd4c3dbdda68c5.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号