我正在尝试使用 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/