单接口方法时候,只有一个参数,无返回值的情况下不需要定义接口,有返回值的时候只需将Unit改为返回值类型
//赋值类 MyReaderCardManager
private lateinit var listener:(cardInfo: IDCard?) -> Unit;
fun setOnIDCardListener(listener: (cardInfo: IDCard?) ->Unit){
this.listener = listener
}
listener.invoke(idCard)
//回调类
MyReaderCardManager.setOnIDCardListener {
if (it==null){
Log.d(TAG, "startZFBFaceService: 读取身份证超时")
idCardResult.postValue(PersonInfoDataError(errorCode = "读取身份证失败", msg = "读取身份证超时"))
}else{
Log.d(TAG, "startZFBFaceService: ${it?.name}")
val infoData = PersonInfoData()
infoData.id = it.id.trim { it <= ' ' }
infoData.name = it.name.trim { it <= ' ' }
infoData.photoDataHexStr = it.photoDataHexStr.trim { it <= ' ' }
idRecognize(it, infoData) //1:1认证比对
}
}
多接口方法回调
定义一个接口
interface IZFBServicesGetCard {
fun onIDCardInfo(cardInfo: IDCard)
fun onSuccess()
fun onFail():Int
fun onLoad(a:Int,b:String)
}
/**
* @author
* @date 2022/8/10 14:16
* @description Builder 创建一个builder简化回调
**/
class Builder: IZFBServicesGetCard {
private lateinit var idCardListener :(cardInfo: IDCard) -> Unit
private lateinit var successListener :() -> Unit
private lateinit var failListener :() -> Int
private lateinit var loadListener :(a: Int,b:String) -> Unit
fun setOnIDCardInfo(listener: (cardInfo: IDCard) -> Unit){
idCardListener = listener
}
fun setOnSuccess(listener: () -> Unit){
successListener = listener
}
fun setOnFail(listener: () -> Int){
failListener = listener
}
fun setOnLoad(listener: (a: Int,b: String) -> Unit){
loadListener = listener
}
override fun onIDCardInfo(cardInfo: IDCard) {
idCardListener.invoke(cardInfo)
}
override fun onSuccess() {
successListener.invoke()
}
override fun onFail(): Int {
failListener.invoke()
return 6666
}
override fun onLoad(a: Int, b: String) {
loadListener.invoke(a,b)
}
}
//赋值类 MyReaderCardManager
private lateinit var listen: IZFBServicesGetCard
fun setOnZFBServiceListener(listen: Builder.() -> Unit ){
var builder = Builder()
builder.listen()
this.listen = builder
}
listen.onFail()
listen.onSuccess()
listen.onLoad(22222,"456")
//监听类
MyReaderCardManager.setOnZFBServiceListener {
setOnFail {
Log.d(TAG, "startZFBFaceService: setOnFail")
}
setOnIDCardInfo {
Log.d(TAG, "startZFBFaceService: setOnIDCardInfo${it.name}")
}
setOnLoad { a, b ->
Log.d(TAG, "startZFBFaceService: setOnLoad --$a+++$b")
}
setOnSuccess {
Log.d(TAG, "startZFBFaceService: setOnSuccess")
}
}