Android中kotlin的学习(anko + kotlin)

转载请标明出处: 
http://blog.csdn.net/yuguo_tianqing/article/details/78191438
本文出自YuGuo_TianQing的博客


一、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布局。

[plain] view plain copy
  1. class AnkoActivity : AppCompatActivity() {  
  2.   
  3.     override fun onCreate(savedInstanceState: Bundle?) {  
  4.         super.onCreate(savedInstanceState)  
  5.   
  6.         verticalLayout {  
  7.   
  8.             val name = editText {  
  9.                 hint = getString(R.string.button_hint)  
  10.                 background = null  
  11.             }  
  12.   
  13.             button("Say Hello") {  
  14.                 onClick { toast("Hello,${name.text}!") }  
  15.                 backgroundColor = ContextCompat.getColor([email protected], R.color.colorPrimary)  
  16.             }.lparams(width = wrapContent, height = wrapContent) {  
  17.                 gravity = Gravity.CENTER  
  18.             }  
  19.   
  20.         }  
  21.   
  22.     }  
  23.   
  24. }  

效果图:

Android中kotlin的学习(anko + kotlin)


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


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

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

activity:

[plain] view plain copy
  1. class AnkoActivity : AppCompatActivity() {  
  2.   
  3.     override fun onCreate(savedInstanceState: Bundle?) {  
  4.         super.onCreate(savedInstanceState)  
  5.   
  6.         val str = arrayOfNulls<String>(20)  
  7.         for (i in 0..19) {  
  8.             str[i] = i.toString()  
  9.         }  
  10.   
  11.         verticalLayout {  
  12.   
  13.             val name = editText {  
  14.                 hint = getString(R.string.button_hint) // <string name="button_hint">你为何这么叼</string>  
  15.                 background = null  
  16.             }  
  17.   
  18.             button("Say Hello") {  
  19.                 onClick { toast("Hello,${name.text}!") }  
  20.                 backgroundColor = ContextCompat.getColor([email protected], R.color.colorPrimary)  
  21.             }.lparams(width = wrapContent, height = wrapContent) {  
  22.                 gravity = Gravity.CENTER  
  23.             }  
  24.   
  25.             recyclerView {  
  26.                 backgroundColor = ContextCompat.getColor([email protected], android.R.color.white)  
  27.                 layoutManager = LinearLayoutManager([email protected], LinearLayoutManager.VERTICAL, false)  
  28.                 val adapter = MainAdapter([email protected], str)  
  29.                 this.adapter = adapter  
  30.                 adapter.setOnItemClickListener(object : OnItemClickListener {  
  31.                     override fun onclick(v: View, position: Int) {  
  32.                         toast(position.toString())  
  33.                     }  
  34.                 })  
  35.             }  
  36.   
  37.   
  38.         }  
  39.   
  40.     }  
  41.   
  42. }  


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

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

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


 效果图:


Android中kotlin的学习(anko + kotlin)


四、加入 SwipeRefreshLayout :

[plain] view plain copy
  1. class AnkoActivity : AppCompatActivity() {  
  2.   
  3.     override fun onCreate(savedInstanceState: Bundle?) {  
  4.         super.onCreate(savedInstanceState)  
  5.   
  6.         val str = arrayOfNulls<String>(20)  
  7.         for (i in 0..19) {  
  8.             str[i] = i.toString()  
  9.         }  
  10.   
  11.         verticalLayout {  
  12.   
  13.             val name = editText {  
  14.                 hint = getString(R.string.button_hint)  
  15.                 background = null  
  16.             }  
  17.   
  18.             button("Say Hello") {  
  19.                 onClick { toast("Hello,${name.text}!") }  
  20.                 backgroundColor = ContextCompat.getColor([email protected], R.color.colorPrimary)  
  21.             }.lparams(width = wrapContent, height = wrapContent) {  
  22.                 gravity = Gravity.CENTER  
  23.             }  
  24.   
  25.             swipeRefreshLayout {  
  26.                 setOnRefreshListener {  
  27.                     Timer().schedule(object : TimerTask() { // 模拟网络请求  
  28.                         override fun run() {  
  29.                             runOnUiThread {  
  30.                                 isRefreshing = false  
  31.                                 toast("22222222")  
  32.                             }  
  33.                         }  
  34.                     }, 2500)  
  35.                 }  
  36.   
  37.                 recyclerView {  
  38.                     backgroundColor = ContextCompat.getColor([email protected], android.R.color.white)  
  39.                     layoutManager = LinearLayoutManager([email protected], LinearLayoutManager.VERTICAL, false)  
  40.                     val adapter = MainAdapter([email protected], str)  
  41.                     this.adapter = adapter  
  42.                     adapter.setOnItemClickListener(object : OnItemClickListener {  
  43.                         override fun onclick(v: View, position: Int) {  
  44.                             toast(position.toString())  
  45.                         }  
  46.                     })  
  47.                 }  
  48.             }  
  49.   
  50.         }  
  51.   
  52.     }  
  53.   
  54. }  

效果图:

Android中kotlin的学习(anko + kotlin)


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

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

[plain] view plain copy
  1. class AnkoActivity : AppCompatActivity() {  
  2.   
  3.     override fun onCreate(savedInstanceState: Bundle?) {  
  4.         super.onCreate(savedInstanceState)  
  5.   
  6.         val str = arrayOfNulls<String>(20)  
  7.         for (i in 0..19) {  
  8.             str[i] = i.toString()  
  9.         }  
  10.   
  11.         verticalLayout {  
  12.             backgroundColor = ContextCompat.getColor([email protected], R.color.colorAccent) // 背景色  
  13.   
  14.             val name = editText {  
  15.                 hint = getString(R.string.button_hint)  // <string name="button_hint">你为何这么叼</string>  
  16.                 background = null  
  17.             }  
  18.   
  19.             button("Say Hello") {  
  20.                 onClick { toast("Hello,${name.text}!") }   // 点击事件  
  21.                 backgroundColor = ContextCompat.getColor([email protected], R.color.colorPrimary)  
  22.             }.lparams(width = wrapContent, height = wrapContent) {  
  23.                 gravity = Gravity.CENTER  
  24.             }  
  25.   
  26.             swipeRefreshLayout {  
  27.                 setOnRefreshListener {  
  28.                     Timer().schedule(object : TimerTask() {    // 模拟网络请求  
  29.                         override fun run() {  
  30.                             runOnUiThread {  
  31.                                 isRefreshing = false  
  32.                                 toast("22222222")  
  33.                             }  
  34.                         }  
  35.                     }, 2500)  
  36.                 }  
  37.   
  38.                 recyclerView {  
  39.                     backgroundColor = ContextCompat.getColor([email protected], android.R.color.white) // 背景色  
  40.                     layoutManager = LinearLayoutManager([email protected], LinearLayoutManager.VERTICAL, false)  
  41.                     val adapter = MainAdapter([email protected], str)  
  42.                     this.adapter = adapter  
  43.                     adapter.setOnItemClickListener(object : OnItemClickListener {  
  44.                         override fun onclick(v: View, position: Int) {  
  45.                             toast(position.toString())  
  46.                         }  
  47.                     })  
  48.                 }  
  49.             }.lparams(width = matchParent, height = dip(300))  
  50.   
  51.             verticalLayout {  
  52.                 orientation = LinearLayout.HORIZONTAL  
  53.   
  54.                 textView("我是第一") {  
  55.   
  56.                 }  
  57.   
  58.                 textView("我是第二") {  
  59.   
  60.                 }  
  61.             }  
  62.         }  
  63.   
  64.     }  
  65.   
  66. }  

效果图:

Android中kotlin的学习(anko + kotlin)



总结:

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

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