Skip to content

emotion toast api for Kotlin

vincent(朱志强) edited this page Jun 8, 2024 · 13 revisions

返回API首页

目录导航

快速上手

普通

回到目录导航

//string msg
SmartToast.emotion().info("当前网络良好")
//resource msg
SmartToast.emotion().info(R.string.net_fine)

警告

回到目录导航

//short toast
SmartToast.emotion().warning("电量过低,请尽快充电")
//short toast
SmartToast.emotion().warning(R.string.power_low)

成功

回到目录导航

//short toast
SmartToast.emotion().success("删除成功")
//short toast
SmartToast.emotion().success(R.string.delete_succ)

错误

回到目录导航

//short toast
SmartToast.emotion().error("数据解析出错")
//short toast
SmartToast.emotion().error(R.string.data_parse_error)

失败

回到目录导航

//short toast
SmartToast.emotion().fail("保存失败")
//short toast
SmartToast.emotion().fail(R.string.save_fail)

完成

回到目录导航

//short toast
SmartToast.emotion().complete("下载完成")
//short toast
SmartToast.emotion().complete(R.string.download_complete)

禁止

回到目录导航

//short toast
SmartToast.emotion().forbid("当前账户不允许汇款操作")

或者,

//short toast
SmartToast.emotion().forbid(R.string.forbid_op)

等待

回到目录导航

//short toast
SmartToast.emotion().waiting("已在后台下载,请耐心等待")

或者,

//short toast
SmartToast.emotion().waiting(R.string.wait_to_download)

配置风格

需要配置风格时,调用config()方法,配置结束后,调用commit方法生效

配置背景

回到目录导航

SmartToast.emotion()
    .config()
    .backgroundDrawable(drawable)
    .commit()
    .info("I'm a toast")

或者,

SmartToast.emotion()
    .config()
    .backgroundResource(R.drawable.toast_bg)
    .commit()
    .info("I'm a toast")

配置背景颜色

回到目录导航
配置颜色,并不会替换默认的背景drawable,只会替换其填充色

SmartToast.emotion()
    .config()
    .backgroundColor(Color.BLUE)
    .commit()
    .info("I'm a toast")

或者,

SmartToast.emotion()
    .config()
    .backgroundColorResource(R.color.colorPrimary)
    .commit()
    .info("I'm a toast")

配置消息文本的风格

回到目录导航

SmartToast.emotion()
    .config()
    //颜色,大小,是否加粗
    .messageStyle(Color.WHITE, 14f, false)
    .commit()
    .info("I'm a toast")

配置Icon

回到目录导航
不设置Icon则为默认的Icon

SmartToast.emotion()
    .config()
    .iconDrawable(drawable)
    .commit()
    .info("I'm a toast")

或者,

SmartToast.emotion()
    .config()
    .iconResource(R.drawable.ic_custom_icon)
    .commit()
    .info("I'm a toast")

配置icon大小

回到目录导航

SmartToast.emotion()
    .config()
    .iconSize(30f)
    .commit()
    .info("I'm a toast")

配置Icon与消息文本的间距

回到目录导航

SmartToast.emotion()
    .config()
    .marginBetweenIconAndMsg(10f)
    .commit()
    .info("I'm a toast")

自定义时长

回到目录导航

SmartToast.emotion()
    .config()
    .duration(Duration.of(5000))
    .commit()
    .info("custom duration")

duration方法的参数必须是Duration对象,有三个内置值:

  • Duration.SHORT 2s的toast,也是不配置duration时的默认值
  • Duration.LONG,3.5s的toast
  • Duration.INDEFINITE 一直展示,直到被下一个toast覆盖,或主动调用SmartToast.dismiss()方法关闭

此外,可通过Duration.of方法,获取任意时长的Duration,单位为毫秒

Duration.of(5000)