我正在尝试在 kotlin 中定义一个 StringDef:
@Retention(AnnotationRetention.SOURCE)
@StringDef(NORTH, SOUTH)
annotation class FilterType {
companion object {
const val NORTH = "NORTH"
const val SOUTH = "SOUTH"
}
}
我认为上面的代码有问题。
// I can send anything to this method, when using my kotlin stringDef
private fun takeString(@DirectionJava.Direction filterType: String) {
我想要下面的 java 的 kotlin 等价物:
public class DirectionJava {
public static final String NORTH = "NORTH";
public static final String SOUTH = "SOUTH";
@Retention(RetentionPolicy.SOURCE)
@StringDef({
NORTH,
SOUTH,
})
public @interface Direction {
}
}
从 kotlin 调用 java 定义的 StringDef 工作
// This works as expected, the Java-written string def
// restricts what I can pass to this method
private fun takeString(@DirectionJava.Direction filterType: String) {
我哪里出错了,你如何在 Kotlin 中定义 StringDef?
最佳答案
根据jetbrains issue , Lint
kotlin 中枚举注释的检查插件正在开发中,还不稳定。检查 android 支持注解,例如 @Nullable
, @StringRes
, @DrawableRes
, @IntRange
, ...(用java编写)工作正常,用户定义的枚举注释没有正确检查。所以,似乎我们应该在 java 中定义它们,然后在 kotlin 中使用。
关于android - 在 kotlin 中定义 StringDef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53777697/