首页 > 解决方案 > 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 中。

可能是什么问题?

标签: androidkotlinone-time-password

解决方案


我猜不再支持 SetListener,尝试使用

 otpView = findViewById(R.id.otp_view)
    otpView!!.setOtpCompletionListener(object :OnOtpCompletionListener
    {
        override fun onOtpCompleted(otp: String?) {

        }

    }) 

推荐阅读