0

如果我在 kotlin 中使用应用程序是否安全enum?像这样:

enum class Labels(title: String, type: Int) {
     PERFORM(App.application.getString(R.string.perform), 0),
     DUTY(App.application.getString(R.string.duty), 1),
     ... ...
}
4

1 回答 1

1

我不会Appenum. 相反,我只会传递资源 id,因为我们不能依赖App在枚举类首次加载时实例化该类:

enum class Labels(val titleResId: Int, val type: Int) {
   PERFORM(R.string.perform, 0),
   DUTY(R.string.duty, 1),
   ... ...
}

稍后我们可以使用它,例如在 中Activity,像这样:

textView.setText(Labels.PERFORM.titleResId)
于 2019-03-26T06:25:55.873 回答