【问题标题】:OTP Input in Kotlin - library android-otpview-pinviewKotlin 中的 OTP 输入 - 库 android-otpview-pinview
【发布时间】:2019-11-01 07:52:53
【问题描述】:

我在我的 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 中。

可能是什么问题?

【问题讨论】:

  • 尝试从 OnOtpCompletionListener() 中删除 ()
  • 好的,没关系。未解决的参考:setListener 呢?
  • 如果你已经删除了,那么现在有什么问题?
  • 错误:该类没有 cnostructor GONE,但错误:未解决存在..

标签: android kotlin one-time-password


【解决方案1】:

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

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

        }

    }) 

【讨论】:

  • 我已经这样做了: otpView?.setOtpCompletionListener(object:OnOtpCompletionListener { override fun onOtpCompleted(otp:String) { 而且它也在工作。哪个更好?
  • !!如果值为空,则通过异常,?将返回 null。
猜你喜欢
  • 2019-03-13
  • 2022-08-16
  • 2018-07-23
  • 1970-01-01
  • 2019-10-01
  • 2021-04-12
  • 2016-08-06
  • 2022-08-16
  • 2022-11-13
相关资源
最近更新 更多