简体   繁体   English

如何在 Kotlin 中正确使用 setOnLongClickListener()

[英]How to properly use setOnLongClickListener() with Kotlin

I've been trying to set up a long click listener event, but keep getting the following error:我一直在尝试设置一个长按侦听器事件,但不断收到以下错误:

Type mismatch. 

Required:Boolean

Found:Unit

I've had no issues with the setOnClickListener event, but for some reason I'm having zero luck with the setOnLongClickListener event.我对setOnClickListener事件没有任何问题,但由于某种原因,我对setOnLongClickListener事件的运气为零。

I'm currently trying to display a simple Toast :我目前正在尝试显示一个简单的Toast

view.setOnLongClickListener{
    Toast.makeText(this, "Long click detected", Toast.LENGTH_SHORT).show();
}

I've seen plenty of examples for Java, but I'm yet to find any examples for Kotlin.我已经看到了很多 Java 的例子,但我还没有找到任何 Kotlin 的例子。

OnLongClickListener.onLongClick signature required that you return a boolean to notify if you actually consumed the event OnLongClickListener.onLongClick签名要求您返回一个布尔值以通知您是否实际使用了该事件

view.setOnLongClickListener{
     Toast.makeText(this, "Long click detected", Toast.LENGTH_SHORT).show()
     return@setOnLongClickListener true
}

or

view.setOnLongClickListener{
     Toast.makeText(this, "Long click detected", Toast.LENGTH_SHORT).show()
     true
}

Another way can be this...另一种方式可以是这样......

view.setOnLongClickListener{
    dispathAnEventOnLongClick("Long click detected!");
}
private fun dispathAnEventOnLongClick(text:CharSequence): Boolean {
    Toast.makeText(applicationContext,text,Toast.LENGTH_SHORT).show();
    return true;
}

Inline Functions内联函数

You can make an inline function that consume a function and return a boolean.您可以创建一个使用函数并返回布尔值的内联函数。 Then use it with any functions that required a boolean as return type.然后将它与任何需要布尔值作为返回类型的函数一起使用。

In a kotlin file:在 kotlin 文件中:

inline fun consume(function: () -> Unit): Boolean {
    function()
    return true
}

Usage:用法:

view.setOnLongClickListener {
   consume { Toast.makeText(context, "Long click detected", Toast.LENGTH_SHORT).show() }
}

Now your code will work and returned a true value to satisfied the need for setOnLongClickListener method.现在您的代码将工作并返回一个真值以满足setOnLongClickListener方法的需要。 You can reuse this function consume with any function that require a true value like onCreateOptionsMenu and onOptionsItemSelected without an explicitly needs to return a true value.您可以将此函数与任何需要真值的函数一起consume ,例如onCreateOptionsMenuonOptionsItemSelected而无需明确需要返回真值。

This way uses: Inline Functions .这种方式使用:内联函数 And the solution you checked as a best answer uses : Labeled Return .您检查为最佳答案的解决方案使用: Labeled Return

I did like this.我就是喜欢这个。

Inside onCreate,在 onCreate 里面,

    findViewById<Button>(R.id.myButton).setOnLongClickListener(myButtonLongClickListener)

And then,然后,

private var timeButtonLongClickListener = View.OnLongClickListener {
    true
}

This one also works for Kotlin.这个也适用于 Kotlin。 Simply return true简单地返回true

view.setOnLongClickListener {
    Toast.makeText(this,"This is a long click",Toast.LENGTH_SHORT).show(); 
    true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM