【问题标题】:Android resource annotations on a list列表中的 Android 资源注释
【发布时间】:2019-05-15 23:13:47
【问题描述】:

有没有办法用资源注释来注释整数列表,以便当我错误地使用方法的整数返回值时,lint 会向我显示错误?

示例(setTextColor() 只需要@ColorInt):

fun useColor() {
    val textView = TextView(null)
    textView.setTextColor(getColor()) // This correctly shows an error
    textView.setTextColor(getColorList()[0]) // Can I make this show an error too? 
}

@ColorRes
fun getColor() : Int {
    return R.color.black
}

@ColorRes // any way to specify this differently?
fun getColorList() : List<Int>{
    return arrayListOf(R.color.black)
}

【问题讨论】:

    标签: android kotlin annotations lint


    【解决方案1】:

    我刚才确实有点动用了我的大脑,这是我的解决方法。不回答问题,但在一定程度上解决了问题。只需将返回值减少为单个值:

    fun useColor() {
        val textView = TextView(null)
        textView.setTextColor(getColorValue(0)) // Correctly show error
    }
    
    @ColorRes
    fun getColorValue(index : Int) : Int {
        return arrayListOf(R.color.black)[index]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 2012-07-02
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多