Pav*_*lev 28 printing android kotlin
我需要使用Kotlin将一些str打印到控制台(Android Studio).我试过了:
Log.v()
Log.d()
Log.i()
Log.w()
Log.e()
Run Code Online (Sandbox Code Playgroud)
方法.但它似乎只适用于Java.我应该用Kotlin打印什么?谢谢
Ram*_*lus 51
有几种方法.
Log.d("TAG", "message");例如,您可以使用,但首先需要导入Log.
import android.util.Log
{...}
Log.d("TAG", "message")
{...}
Run Code Online (Sandbox Code Playgroud)
资料来源:https://developer.android.com/reference/android/util/Log.html
您还可以使用kotlin的print和println功能.
例:
{...}
print("message")
println("other message")
{...}
Run Code Online (Sandbox Code Playgroud)
资料来源:https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/
此时(带有 Kotlin 插件的 android studio 2.3.3),
Log.i(TAG, "Hello World")
Run Code Online (Sandbox Code Playgroud)
就可以了。它将导入android.util.Log
我编写了一些扩展函数,它们使用具体化的类型参数,以避免在所有项目的类中声明日志标记。以下代码段显示了基本思想:
inline fun <reified T> T.logi(message: String) = Log.i(T::class.java.simpleName, message)
Run Code Online (Sandbox Code Playgroud)
基本上,您可以使用以下调用(W/O 外部依赖项)将某些内容记录到 logcat:
logi("My log message")
Run Code Online (Sandbox Code Playgroud)
你可以在这里找到一个要点。要点中声明的函数更详细一点,因为它们允许:
androidKotlin已被弃用,并使用安口来代替。
https://github.com/Kotlin/anko/wiki/Anko-Commons-%E2%80%93-登录
class SomeActivity : Activity(), AnkoLogger {
private fun someMethod() {
info("London is the capital of Great Britain")
debug(5) // .toString() method will be executed
warn(null) // "null" will be printed
}
}
Run Code Online (Sandbox Code Playgroud)