美文网首页
Android Java变道Kotlin(1)

Android Java变道Kotlin(1)

作者: LeoooL | 来源:发表于2017-05-26 17:34 被阅读0次

1.监听函数

函数式

 findViewById(R.id.btn_hello)?.setOnClickListener{
            Toast.makeText(this,"Hello Kotlin",Toast.LENGTH_LONG).show()
        }

函数作为变量

 val mListener = fun (v:View?){
                 when(v?.id){
                     R.id.btn_hello -> Toast.makeText(this,"Hello Kotlin",Toast.LENGTH_LONG).show()
                 }
         }
 findViewById(R.id.btn_hello)?.setOnClickListener(mListener)

内部类实现View.OnClickListener接口

inner class MyListener:View.OnClickListener{
        override fun onClick(v: View?) {
            when(v?.id){
                //this@MyListener   MyListener's this
                //this@MainActivity MainActivity's this
                R.id.btn_hello -> Toast.makeText(this@MainActivity,"Hello Kotlin",Toast.LENGTH_LONG).show()
            }
        }
    }

findViewById(R.id.btn_hello)?.setOnClickListener(MyListener())

相关文章

网友评论

      本文标题:Android Java变道Kotlin(1)

      本文链接:https://www.haomeiwen.com/subject/fhpxfxtx.html