Android中kotlin的学习(anko + kotlin)


一、Anko是什么?

原话是这么说的:

Anko is a Kotlin library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.

基础翻译:anko是一个kotlin library,能使Android应用程序开发更快更容易。让使您的代码简洁,易于阅读,让您忘记Android SDK for java的粗糙边缘。

我也不多说了,直接给github上面的连接:https://github.com/Kotlin/anko

二、它到底能干什么呢?答案是:布局。

直接上代码,让你体会一下它的魅力。基础代码:

在开发之前你必须要: compile "org.jetbrains.anko:anko:$anko_version" , 详细情况github上面也已经说明。

首先创建了一个AnkoActivity,并在AndroidManifest.xml中声明了。然后就开始我们的anko布局。

class AnkoActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        verticalLayout {

            val name = editText {
                hint = getString(R.string.button_hint)
                background = null
            }

            button("Say Hello") {
                onClick { toast("Hello,${name.text}!") }
                backgroundColor = ContextCompat.getColor(this@AnkoActivity, R.color.colorPrimary)
            }.lparams(width = wrapContent, height = wrapContent) {
                gravity = Gravity.CENTER
            }

        }

    }

}

效果图:



看了上面的代码和效果图之后,感觉如何? 嘿嘿........


三、接下来,我们再加入一些内容,比如 RecyclerView 应该怎么来操作。

在开发之前一定要记得先: compile "org.jetbrains.anko:anko-recyclerview-v7:$anko_version"

activity:

class AnkoActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val str = arrayOfNulls<String>(20)
        for (i in 0..19) {
            str[i] = i.toString()
        }

        verticalLayout {

            val name = editText {
                hint = getString(R.string.button_hint) // <string name="button_hint">你为何这么叼</string>
                background = null
            }

            button("Say Hello") {
                onClick { toast("Hello,${name.text}!") }
                backgroundColor = ContextCompat.getColor(this@AnkoActivity, R.color.colorPrimary)
            }.lparams(width = wrapContent, height = wrapContent) {
                gravity = Gravity.CENTER
            }

            recyclerView {
                backgroundColor = ContextCompat.getColor(this@AnkoActivity, android.R.color.white)
                layoutManager = LinearLayoutManager(this@AnkoActivity, LinearLayoutManager.VERTICAL, false)
                val adapter = MainAdapter(this@AnkoActivity, str)
                this.adapter = adapter
                adapter.setOnItemClickListener(object : OnItemClickListener {
                    override fun onclick(v: View, position: Int) {
                        toast(position.toString())
                    }
                })
            }


        }

    }

}


这里有一个MainAdapter,这个adapter在我的上篇文章中写得有,链接: http://blog.csdn.net/yuguo_tianqing/article/details/73277095

有一点不一样,其它的都一样,我把不一样的地方贴出来:

class MainAdapter(val context: Context, val data: Array<String?>) : RecyclerView.Adapter<MainAdapter.MyViewHolder>() {
多了一个问号,因为传递的数据我是这样定义的:val str = arrayOfNulls<String>(20), 使用空值初始化的。


 效果图:





四、加入 SwipeRefreshLayout :

class AnkoActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val str = arrayOfNulls<String>(20)
        for (i in 0..19) {
            str[i] = i.toString()
        }

        verticalLayout {

            val name = editText {
                hint = getString(R.string.button_hint)
                background = null
            }

            button("Say Hello") {
                onClick { toast("Hello,${name.text}!") }
                backgroundColor = ContextCompat.getColor(this@AnkoActivity, R.color.colorPrimary)
            }.lparams(width = wrapContent, height = wrapContent) {
                gravity = Gravity.CENTER
            }

            swipeRefreshLayout {
                setOnRefreshListener {
                    Timer().schedule(object : TimerTask() { // 模拟网络请求
                        override fun run() {
                            runOnUiThread {
                                isRefreshing = false
                                toast("22222222")
                            }
                        }
                    }, 2500)
                }

                recyclerView {
                    backgroundColor = ContextCompat.getColor(this@AnkoActivity, android.R.color.white)
                    layoutManager = LinearLayoutManager(this@AnkoActivity, LinearLayoutManager.VERTICAL, false)
                    val adapter = MainAdapter(this@AnkoActivity, str)
                    this.adapter = adapter
                    adapter.setOnItemClickListener(object : OnItemClickListener {
                        override fun onclick(v: View, position: Int) {
                            toast(position.toString())
                        }
                    })
                }
            }

        }

    }

}

效果图:



五、看到这里,很多人有疑问,使用verticalLayout { }  默认orientation为vertical, 那么 怎么定义这个属性呢。

接下来我贴上所有的代码以及:

class AnkoActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val str = arrayOfNulls<String>(20)
        for (i in 0..19) {
            str[i] = i.toString()
        }

        verticalLayout {
            backgroundColor = ContextCompat.getColor(this@AnkoActivity, R.color.colorAccent) // 背景色

            val name = editText {
                hint = getString(R.string.button_hint)  // <string name="button_hint">你为何这么叼</string>
                background = null
            }

            button("Say Hello") {
                onClick { toast("Hello,${name.text}!") }   // 点击事件
                backgroundColor = ContextCompat.getColor(this@AnkoActivity, R.color.colorPrimary)
            }.lparams(width = wrapContent, height = wrapContent) {
                gravity = Gravity.CENTER
            }

            swipeRefreshLayout {
                setOnRefreshListener {
                    Timer().schedule(object : TimerTask() {    // 模拟网络请求
                        override fun run() {
                            runOnUiThread {
                                isRefreshing = false
                                toast("22222222")
                            }
                        }
                    }, 2500)
                }

                recyclerView {
                    backgroundColor = ContextCompat.getColor(this@AnkoActivity, android.R.color.white) // 背景色
                    layoutManager = LinearLayoutManager(this@AnkoActivity, LinearLayoutManager.VERTICAL, false)
                    val adapter = MainAdapter(this@AnkoActivity, str)
                    this.adapter = adapter
                    adapter.setOnItemClickListener(object : OnItemClickListener {
                        override fun onclick(v: View, position: Int) {
                            toast(position.toString())
                        }
                    })
                }
            }.lparams(width = matchParent, height = dip(300))

            verticalLayout {
                orientation = LinearLayout.HORIZONTAL

                textView("我是第一") {

                }

                textView("我是第二") {

                }
            }
        }

    }

}

效果图:




总结:

前面的都是一些基本的应用。anko的运用中,肯定还可以自定义控件,同时你只需自定义一个,其它都可以进行引用。等等·······功能

这一章就到这里了, 也不太善言辞,不对的地方请指出,我及时修正,我会继续进步的,争取写出好的文章。


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值