android - 有没有办法在 fragment 中使用Anko commons?

标签 android kotlin anko

enter image description here

我正在尝试使用 Anko 库 调用 toast{..} 函数。在 Activity 中调用 toast{...} 函数很简单,但我找不到在 Fragment 中调用该函数的方法。

那么,有没有办法在 fragment 中使用 Anko commons ?

最佳答案

如果你浏览 Anko 的文档 toast{..} ,其实现为:

/**
 * Display the simple Toast message with the [Toast.LENGTH_SHORT] duration.
 *
 * @param message the message text resource.
 */

inline fun Context.toast(message: Int): Toast = Toast
        .makeText(this, message, Toast.LENGTH_SHORT)
        .apply {
            show()
        }

toast{..}Context 的扩展函数类(class)。因此,只能从继承 Context 的类调用它。类。

所以,使用toast{...}在您的 fragment 中,您必须使用 activity?.toast("YOUR_TOAST_MESSAGE_HERE") .

关于android - 有没有办法在 fragment 中使用Anko commons?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54454464/

相关文章:

android - 访问 Activity 的标准选项菜单

android - Firebase runloop (3.0.0) 中的未捕获异常

android - Kotlin-如何知道editText是否失去焦点

java - DialogFragment 正在将 XML 扩展到全屏?

kotlin - 在 kotlin 1.3 中使用协程的正确方法

android - MediaCodec 在每个视频帧上录制带有时间戳的视频

java - 如何检索以逗号作为分隔符的空格行?

Spring Kotlin DSL : get all beans of certain type

kotlin - 在Kotlin Anko中使用ConstraintLayout

android - Kotlin/anko 多个异步任务