体验:Anko + Kotlin

● 依赖:compile 'org.jetbrains.anko:anko:0.10.0'
● 界面:
import android.view.View
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk25.coroutines.onClick

class MainActivityUI : AnkoComponent<MainActivity> {

    override fun createView(ui: AnkoContext<MainActivity>): View = with(ui) {
        verticalLayout {
            padding = dip(10)
            var name = textView("Hello World") {
                padding = dip(10)
            }
            button("更换文字") {
                onClick { name.text = "Anko Demo" }
            }
        }
    }
}
● Activity:
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import org.jetbrains.anko.setContentView

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MainActivityUI().setContentView(this)
    }
}
posted @ 2017-07-10 12:00  祁连山  阅读(957)  评论(0编辑  收藏  举报