【问题标题】:Can't pass random integer when calling makeText()调用 makeText() 时无法传递随机整数
【发布时间】:2016-09-26 03:09:44
【问题描述】:

我是 android 新手,正在玩 makeText 类的 makeText 方法。我注意到在调用 makeText 时,除了TOAST.LENGTH_LONGTOAST.LENGTH_SHORT 之外,我无法为持续时间参数传递整数。 Android studio 警告说它已经用@intDef 进行了注释,但是当我打开 Toast 类的源代码时,我找不到参数注释的位置。知道这是如何执行的吗?

我的目标 SDK 版本是API 19: Android 4.4 (KitKat)signature 是:

public static Toast makeText(Context context, CharSequence text, int 持续时间)

【问题讨论】:

  • 你编译的 API 版本是什么?
  • @Code-Apprentice : 我的目标 SDK 版本是 API 19
  • public static Toast makeText(Context context, CharSequence text, @Duration int duration)
  • @k0sh 对我来说是:public static Toast makeText(Context context, CharSequence text, int duration)
  • 是的,你在问题@Kode 中写了makeToast

标签: java android toast android-toast


【解决方案1】:

在 Android 5.1.1 中,Toast.makeText()duration 参数使用 @Duration 注释。这个注解又用@IntDef注解。

【讨论】:

  • 为什么我看不到呢?如果 API 19 没有对其进行注释,我为什么会收到错误消息?对不起,我有点困惑。您能否在答案中添加更多解释?
【解决方案2】:

下面的代码对我有用

     final int SHORT_DELAY = 2000; // 2 seconds

  Toast.makeText(getActivity(),String.valueOf(12314),SHORT_DELAY).show();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    相关资源
    最近更新 更多