相关疑难解决方法(0)

为什么从Android的性能提示中删除了"避免只需要Ints的枚举"?

从官方开发者文档中删除了"避免只需要Ints的枚举"部分.(请参阅为什么Android不使用更多枚举?对于旧版内容)

为什么?是否有Android VM的更改使得提示过时了?

enums android

171
推荐指数
6
解决办法
3万
查看次数

如何在kotlin中使用Android支持typedef注释?

我开发Android应用程序并经常使用注释作为编译时参数检查,主要是android的支持注释.

java代码中的示例:

public class Test
{
    @IntDef({Speed.SLOW,Speed.NORMAL,Speed.FAST})
    public @interface Speed
    {
         public static final int SLOW = 0;
         public static final int NORMAL = 1;
         public static final int FAST = 2;
    }

    @Speed
    private int speed;

    public void setSpeed(@Speed int speed)
    {
        this.speed = speed;
    }
}
Run Code Online (Sandbox Code Playgroud)

由于Android中的性能问题,我不想使用枚举.kotlin的自动转换器只生成无效代码.如何@IntDef在kotlin中使用注释?

android kotlin

33
推荐指数
3
解决办法
9369
查看次数

如何在 Kotlin 中使用 StringDef 或 IntDef?

参考https://developer.android.com/reference/android/support/annotation/StringDef https://developer.android.com/reference/android/support/annotation/IntDef

我可以轻松创建我的编译验证,将我的字符串参数限制为特定类型的字符串(在 Java 中)

例如

import android.support.annotation.StringDef;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.SOURCE;

@Retention(SOURCE)
@StringDef({
    "allow_one",
    "okay_two"
})
public @interface AllowedString { }
Run Code Online (Sandbox Code Playgroud)

所以如果我有

class TestAnnotation(@AllowedString private val name: String) {
    fun printName(@AllowedString name: String) {}
}
Run Code Online (Sandbox Code Playgroud)

当我编码时

val testAnnotation = TestAnnotation("not_allowed")
Run Code Online (Sandbox Code Playgroud)

Android Studio 将标记错误,not_allowed因为它不在列表中。

如果我将AllowedString注释界面转换为 Kotlin ,如下所示,它将不再起作用。为什么?

import android.support.annotation.StringDef
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy.SOURCE

@Retention(SOURCE)
@StringDef("allow_one", "okay_two")
annotation class AllowedString
Run Code Online (Sandbox Code Playgroud)

为什么?

android annotations kotlin

5
推荐指数
0
解决办法
3373
查看次数

标签 统计

android ×3

kotlin ×2

annotations ×1

enums ×1