Java 如何在 Kotlin 中使用回调?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47499891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I can use callback in Kotlin?
提问by manwhotrycoding
I have View and one CircleShape , which should show toast in this View. And I use it in main Activity. This is my interface
我有 View 和一个 CircleShape ,它应该在这个视图中显示吐司。我在主活动中使用它。这是我的界面
interface OnClickListenerInterface {
fun onClick()
}
It is CircleShape( it is View in my xml) and listener in my View. I want to implement OnClick in my Activity.
它是 CircleShape(它是我的 xml 中的视图)和我的视图中的侦听器。我想在我的活动中实现 OnClick。
var listener: OnClickListenerInterface? = null
mCircleShape.setOnClickListener(View.OnClickListener {
if (listener == null) return@OnClickListener
listener!!.onClick()
})
I know , that in Kotlin getters and setters generic automatics, but how I can set listener if it private. It is code from my Activity, but It doesn't work
我知道,在 Kotlin 中的 getter 和 setter 通用自动程序,但是如果它是私有的,我如何设置侦听器。这是来自我的活动的代码,但它不起作用
CircleShape.listener = object :OnClickListenerInterface{
override fun onClick() {
ToastUtils.showSuccessMessage(getContext(),"pressed")
}
}
How I should to use Callback, onClickListenere in Kotlin?
我应该如何在 Kotlin 中使用回调、onClickListenere?
回答by Yevhenii Kanivets
First of all you need to remove this code:
首先,您需要删除此代码:
mCircleShape.setOnClickListener(View.OnClickListener {
if (listener == null) return@OnClickListener
listener!!.onClick()
})
Because listener is always null at first and your code just always returns.
因为 listener 一开始总是 null 并且您的代码总是返回。
var listener: OnClickListenerInterface? = null
is already public (this is a default access level in Kotlin). So you can just set it in your activity, when needed. Use listener?.onClick()
call to trigger it from your CircleShape.
var listener: OnClickListenerInterface? = null
已经公开(这是 Kotlin 中的默认访问级别)。因此,您可以在需要时将其设置在您的活动中。使用listener?.onClick()
call 从您的 CircleShape 触发它。
回答by Gabriel Janucik
Have you tried to use a lambda expression? For example in your case:
您是否尝试过使用 lambda 表达式?例如在你的情况下:
mCircleShape.setOnClickListener(
{ _ -> ToastUtils.showSuccessMessage(context,"pressed") }
)
Or if you want to make it more kotlin style:
或者,如果你想让它更像 kotlin 风格:
mCircleShape.listener = (
{ _ -> ToastUtils.showSuccessMessage(context,"pressed") }
)
回答by dr3k
On CircleShape.kt.
在 CircleShape.kt 上。
private listener OnClickListenerInterface? = null
...
fun setOnClickListener(listener: OnClickListenerInterface){
this.listener = listener
}
On your Activity
在您的活动中
mCircleShape.setOnClickListener(object: CircleShape.OnClickListenerInterface {
override fun onClick(){ // Do something here
}
}
If you're gonna use lambda expression, you can use a Function Type. Here how it looks like on CirclesShapt.kt
如果要使用 lambda 表达式,则可以使用函数类型。这是 CirclesShapt.kt 上的样子
fun setOnClickListener(listener: () -> Unit){
listener() // or you could use optional if the lister is nullable "listener?.invoke()"
}
So in activity looks like.
所以在活动中看起来像。
mCircleShape.setOnClickListener {
// Do something here
}
回答by Weidian Huang
A more simpler solution by using lambda.
使用 lambda 的更简单的解决方案。
Inside CircleShape.kt, declare a lambda function.
在 CircleShape.kt 中,声明一个 lambda 函数。
var listener: (()->Unit)? = null
...
// When you want to invoke the listener
listener?.invoke()
Inside your Activity
在您的活动中
mCircleShape.listener = {
// Do something when you observed a call
}
回答by Rishabh Shukla
to use Kotlin callbacks , I use them in my api calls for success or failure use
要使用 Kotlin 回调,我在成功或失败使用的 api 调用中使用它们
create enum class for state
为状态创建枚举类
enum class APIState(val result: Boolean) {
SUCCESS(true),
FAILURE(false)}
use call back in fun
在乐趣中使用回调
private fun fetchFeesList(studentID:String,call:(APIState)->Unit){
... do stuff here , and use call(APIState.SUCCESS) or call(APIState.FAILURE) }
when calling function fetchFeesList , call it like
当调用函数 fetchFeesList 时,像这样调用它
fetchFeesList(studentID){
val res = it.result
if(res){
toast("success")
}else {
toast("failure")
}
}
for toast("message") , use Anko Lib from GitHub : - https://github.com/Kotlin/anko
对于 toast("message") ,使用来自 GitHub 的 Anko Lib: - https://github.com/Kotlin/anko
回答by Dan Alboteanu
define a function like this:
定义一个这样的函数:
fun performWork(param1: String, myCallback: (result: String?) -> Unit) {
// perform some network work
// on network finished
myCallback.invoke("result from network")
}
use like this:
像这样使用:
performWork("http://..."){ result ->
//use result
}