Java如何调用kotlin扩展方法
我用kotlin写了一个扩展方法
package com.zhongan.zachat.extention import android.content.Context import android.widget.Toast /** * Created by Carl on 2016/12/1. * * */ fun Context.toastLong(msg:String) = Toast.makeText(this,msg,Toast.LENGTH_LONG).show() fun Context.toastshort(msg:String) = Toast.makeText(this,msg,Toast.LENGTH_SHORT).show()
当我在kotlin activity
调用toastLong("test")
是可以的。
但是在java actvity
IDE中却说找不到这个方法。
如何在java代码中调用kotlin扩展方法
基于此页面
扩展实际上并不修改它们扩展的类。
需要注意的是扩展名不能从对象类中调用,因为原来的类仍然是一样的。 (所以上下文不会神奇地有一个额外的功能,因此它不能使用Java中的Context.functionName调用)
您应该能够使用以下方式调用它:
com.zhongan.zachat.extention.<fileName>.toastLong(ctx,"string")
例如,如果该文件被称为kotlinFile.kt:
com.zhongan.zachat.extention.KotlinFileKt.toastLong(ctx,"string")