代码之家  ›  专栏  ›  技术社区  ›  Alexa289

有没有在片段中使用Anko Commons的方法?

  •  1
  • Alexa289  · 技术社区  · 7 年前

    enter image description here

    我正在打电话 toast{..} 使用函数 Anko library . 简单地说 toast{...} 活动中的函数,但我找不到调用片段内函数的方法。

    那么,有没有一种方法可以在片段中使用anko commons?

    2 回复  |  直到 7 年前
        1
  •  2
  •   ankuranurag2    7 年前

    如果你查阅了安科的文件 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()
            }
    

    这个 吐司{ } 是的扩展函数 Context 班级。因此,只能从继承自 语境 班级。

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

        2
  •  0
  •   Praveen    7 年前

    试试这个

    实现这一点

     implementation "org.jetbrains.anko:anko:0.10.6"
    

    导入到片段中

    import org.jetbrains.anko.support.v4.toast
    

    在您的代码中

    toast("string")
    

    它对我有效