在Kotlin中实例化接口侦听器

Jos*_*iro 0 android interface kotlin

我一生都无法在Kotlin或Android的Kotlin中实例化片段之外的接口。这是Java中的标准过程,其内容如下:

MyInterface mInterfaceListener = new MyInterface(this);
mInterfaceListener.invokeSomeGenericMethod();
Run Code Online (Sandbox Code Playgroud)

请注意,mInterfaceListener是指接口,而不是onCLickListener或类似的东西

如何在Kotlin中实例化接口?如何制作“监听器”并触发界面功能?

以下是我出于学习目的而在一个非常简单的应用程序中进行的一些尝试。注意变量mPresenterListener是一个接口

    class QuoteActivity : QuoteContract.ViewOps, AppCompatActivity() {

    private lateinit var vText: TextView
    private lateinit var vFab: FloatingActionButton
    private lateinit var mPresenterListener: QuoteContract.PresenterOperations

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mPresenterListener = this.mPresenterListener
        vText=findViewById(R.id.main_quote)
        vFab=findViewById(R.id.main_fab)
        vFab.setOnClickListener{
            mPresenterListener.onQuoteRequested()
        }
    }

    override fun displayQuote(quote: String) {
        vText.text = quote
    }

}
Run Code Online (Sandbox Code Playgroud)

而我的主持人:

    class QuotePresenter(private val viewListener: QuoteContract.ViewOps): QuoteContract.PresenterOperations {

    private lateinit var modelListener: QuoteContract.ModelOperations

    init {
        modelListener = this.modelListener
    }


    override fun onQuoteRequested() {
        modelListener.generateQuote()
    }

    override fun onQuoteGenerated(quote: String) {
        viewListener.displayQuote(quote)
    }


}
Run Code Online (Sandbox Code Playgroud)

界面:

interface QuoteContract {



//Methods available to Presenter (Presenter -> View)
interface ViewOps{
    fun displayQuote(quote: String)
}


//Ops offered from presenter to view (Presenter->View)

interface PresenterOperations {
    //Presenter->View
    fun onQuoteRequested()
    //Presenter->Model
    fun onQuoteGenerated(quote: String)
}

//Ops offered from Model to Presenter (Model -> Presenter)
interface ModelOperations {
    fun generateQuote()
}
Run Code Online (Sandbox Code Playgroud)

}

Rob*_*Rob 6

您可以像这样进行观看者/收听者:

val textView: TextView = this.findViewById(R.id.amountEdit)
val watcher = object : TextWatcher {
    override fun afterTextChanged(p0: Editable?) {
        val inputAmount = textView.text.toString
        val amount = if (!inputAmount.isEmpty()) inputAmount.toDouble() else 0.0
        conversionViewModel?.convert(amount)
        }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        println("before text changed called..")
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        println("text changed..")
    }
}
Run Code Online (Sandbox Code Playgroud)

然后:

textView.addTextChangedListener(watcher)
Run Code Online (Sandbox Code Playgroud)

请注意在声明观察者时的对象。可以用侦听器做同样的事情。

  • 好的,让我了解一下你的方法:Textwatcher 是一个接口,你将它分配给一个单例?这在典型的 MVP 场景中如何运作?谢谢您的回答 (2认同)

小智 5

我们也可以使用没有某些接口且具有默认值的侦听器,例如:

val someButtonListener: (isChecked: Boolean) -> Unit = {_ -> }
val someButtonListener: (v: View) -> Unit = {_ -> }
Run Code Online (Sandbox Code Playgroud)