在不同的Kotlin Android示例中,我看到toast("一些消息...")
或 toastLong("一些长消息")
。例如:
view.setOnClickListener { toast("Click") }
据我所知,这是Activity
的扩展函数。
您需要在何处定义toast()
函数以便在整个项目中都可以使用?
在不同的Kotlin Android示例中,我看到toast("一些消息...")
或 toastLong("一些长消息")
。例如:
view.setOnClickListener { toast("Click") }
据我所知,这是Activity
的扩展函数。
您需要在何处定义toast()
函数以便在整个项目中都可以使用?
它可以是 Context
的扩展函数:
fun Context.toast(message: CharSequence) =
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
你可以将这个代码放在项目的任何位置,具体放在哪里由你自己决定。例如,你可以定义一个文件mypackage.util.ContextExtensions.kt
,并将其作为顶级函数放置在那里。
每当你可以访问Context
实例时,你可以导入该函数并使用它:
import mypackage.util.ContextExtensions.toast
fun myFun(context: Context) {
context.toast("Hello world!")
}
这是Kotlin中的一行解决方案:
Toast.makeText(this@MainActivity, "Its a toast!", Toast.LENGTH_SHORT).show()
this@MainActivity
点了个赞。 - Ravi Vaniya实际上它是 Kotlin 的一个扩展部分 Anko。语法如下:
toast("Hi there!")
toast(R.string.message)
longToast("Wow, such a duration")
在您的应用程序级别的build.gradle
文件中添加 implementation "org.jetbrains.anko:anko-common:0.8.3"
。
将import org.jetbrains.anko.toast
添加到您的Activity中。
试一试
在活动中
Toast.makeText(applicationContext, "Test", Toast.LENGTH_LONG).show()
或者
Toast.makeText(this@MainActiivty, "Test", Toast.LENGTH_LONG).show()
在片段中
Toast.makeText(activity, "Test", Toast.LENGTH_LONG).show()
或者Toast.makeText(activity?.applicationContext, "Test", Toast.LENGTH_LONG).show()
anko
并且想要创建自己的 Toast
扩展,你可以在一个没有类的 kotlin 文件中定义内联(或非内联)扩展,并且你可以在任何类中访问这个函数。inline fun Context.toast(message:String){
Toast.makeText(this, message , duration).show()
}
用法:
在Activity中,
toast("Toast Message")
context?.toast("Toast Message")
import android.content.Context
import android.widget.Toast
import androidx.fragment.app.Fragment
inline fun Context.toast(message:()->String){
Toast.makeText(this, message() , Toast.LENGTH_LONG).show()
}
inline fun Fragment.toast(message:()->String){
Toast.makeText(this.context, message() , Toast.LENGTH_LONG).show()
}
那么你将会拥有
toast {
"Hello World"
}
在片段和活动中都可以使用。
使用Kotlin的Anko时,在fragment中使用以下任一方式:
activity.toast("string message")
或者
context.toast("string message")
或者
view.holder.context.toast("string message")
通过这个Toast扩展函数,你可以在Activity
和Fragment
中调用它们,你可以将this
作为Activity
的Context
或者getApplication()
作为Fragment
的Context
传递进去,默认情况下生成的是Toast.LENGTH_SHORT
,所以你不需要担心要传递它作为参数,但如果需要,你也可以覆盖它。
fun Context.showToast(message: String, duration: Int = Toast.LENGTH_SHORT){
Toast.makeText(context, message , duration).show()
}
showToast("John Doe")
showToast("John Doe", Toast.LENGTH_LONG)
fun Context.toast(message: String, duration: Int = Toast.LENGTH_SHORT){
Toast.makeText(this, message , duration).show()
}
然后调用 toast("John Doe")
或 toast("John Doe", Toast.LENGTH_LONG)
。 - Ian Elvister我已经找到了一个非常简单的方法,可以从给定的链接中进行烤面包操作https://gist.github.com/felipearimateia/ee651e2694c21de2c812063980b89ca3。在这个链接中,使用Activity而不是Context。试试吧。
Android Toast for Kotlin
Activity(活动)
Toast.makeText(applicationContext, "message...", Toast.LENGTH_SHORT).show()
片段
Toast.makeText(activity, "message...", Toast.LENGTH_SHORT).show()