android - 有没有办法在片段中使用 Anko commons?
问题描述
我正在尝试toast{..}
使用Anko library
. 在活动中调用函数很简单toast{...}
,但我找不到在片段中调用函数的方法。
那么,有没有办法在片段中使用 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
。因此,它只能从继承自Context
类的类中调用。
因此,要toast{...}
在您的片段中使用,您必须使用activity?.toast("YOUR_TOAST_MESSAGE_HERE")
.
推荐阅读
- c++ - 预处理字符串连接
- python - 使用公共元素对子列表进行分组
- c# - 仅在 VisualStudio 的调试器中按键对字典进行排序
- variables - 通过在导出 pdf 名称中包含变量来保存到目录路径
- python - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9d in position 0: invalid start byte when I execute the `b.decode()`
- c# - 在 winforms 中显示彩色 3D 框的最简单方法
- c++ - Webots ROS 默认控制器“RosInertialUnit.cpp”中的四元数计算
- python - 我无法在 Django 模板上显示图像
- php - 如何修复 PHP 中的“没有这样的文件或目录”错误
- java - 如何检索对象