android - Kotlin 中的 OTP 输入 - 库 android-otpview-pinview
问题描述
我在我的 Android 应用程序中使用以下库为 OTP 提供 UI:
https://github.com/mukeshsolanki/android-otpview-pinview
成功,添加了如下依赖:
implementation 'com.github.mukeshsolanki:android-otpview-pinview:2.1.0'
此外,添加以下行:
maven { url "https://jitpack.io" }
在布局中使用它,如下所示:
<com.mukesh.OtpView
android:id="@+id/otp_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:inputType="number"
android:itemBackground="@drawable/drawable_otp_entry"
android:textColor="@android:color/white"
app:itemCount="4"
app:lineColor="@color/colorPrimary"
app:viewType="none" />
现在,在我活动的 kotlin 方面,我正在这样做:
class MainActivity : AppCompatActivity() {
var otpView: OtpView? =null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
otpView = findViewById(R.id.otp_view)
otpView.setListener(object:OnOtpCompletionListener() {
override fun onOtpCompleted(otp:String) {
// do Stuff
Log.d("onOtpCompleted=>", otp)
}
})
}
}
错误在行: otpView.setListener(object:OnOtpCompletionListener() {
说未解决的引用:setListener和该类没有构造函数
我正在使用 kotlin,而库可能在 Java 中。
可能是什么问题?
解决方案
我猜不再支持 SetListener,尝试使用
otpView = findViewById(R.id.otp_view)
otpView!!.setOtpCompletionListener(object :OnOtpCompletionListener
{
override fun onOtpCompleted(otp: String?) {
}
})
推荐阅读
- django - Celery 重启导致 RabbitMQ 中的计划任务重新交付
- javascript - 纯 JS 颜色滑块
- cuda - cudaGetLastError。哪个内核执行引发了它?
- javascript - 在 JavaScript 中将节点转换、转换或标记为/作为元素
- algorithm - TCP/IP高效包过滤
- r - ore.frame 元素与标签相连,无法摆脱标签
- c# - 不能在即时窗口中使用 nameof
- c - C 错误:需要左值作为赋值的左操作数
- android-studio - Android Studio 升级想要删除“意外”的 .jks 文件?
- json - 从 Firebase remoteMessage 中的嵌套 JSON 中提取数据