【发布时间】:2020-03-11 22:41:34
【问题描述】:
我有一个用 Kotlin 编写的 Android 应用程序,其类 BaseKeyListener 扩展了 DigitsKeyListener。我的最低 SDK 版本是21。该类当前正在调用已弃用的构造函数。但是,新的构造函数仅在 API 级别 26 及更高级别可用。
如何根据 API 级别有条件地调用构造函数?
我基本上在不久前为 Android 发布了 the same problem,但该解决方案似乎在 Kotlin 中不起作用。
在 Kotlin 中,我的课程现在看起来像这样:
// primary constructor 'DigitsKeyListener' shows lint warning about deprecation.
abstract class BaseKeyListener() : DigitsKeyListener() {
}
如果我为 Android 问题应用解决方案,我会得到以下代码:
abstract class BaseKeyListener : DigitsKeyListener {
// still results in deprecation warning
constructor() : super()
}
如果我必须将构造函数设为私有并实现 newInstance 模式,还提供了一个替代解决方案。但是我不能使用该解决方案,因为还有其他类继承自 BaseKeyListener 并且 BaseKeyListener 也是抽象的。
我唯一能想到的是:
abstract class BaseKeyListener : DigitsKeyListener {
constructor()
@RequiresApi(Build.VERSION_CODES.O)
constructor(locale: Locale) : super(locale)
}
但结果我必须为每个子类定义两个构造函数。如果我使用这个类,我每次都必须添加一个条件,而我们使用的语言环境是相同的。
不幸的结果:
open class AmountKeyListener : BaseKeyListener {
constructor() : super()
@RequiresApi(Build.VERSION_CODES.O)
constructor(locale: Locale) : super(locale)
}
// usage of the keyListener
editText.keyListener = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) KeyListenerUtil.AmountKeyListener(
MY_LOCALE) else KeyListenerUtil.AmountKeyListener()
理想的解决方案应该是将 AmountKeyListener 分配在一行上,并且 BaseKeyListener 应该知道何时使用我们的自定义语言环境“MY_LOCALE”
editText.keyListener = KeyListenerUtil.AmountKeyListener()
如何解决这个问题?
【问题讨论】:
标签: android kotlin constructor deprecated keylistener