Kotlin(一)
Kotlin(一)
写在前面:
- 参考资料:
《Kotlin官方文档》《Kotlin for Android 开源中文版》
- 准备工作:
Android-Studio2.x:添加Kotlin扩展插件,3.x默认支持Kotlin,开箱既得
添加项目依赖(app下的build gradle)exclude group: 'com.android.support', module: 'support-annotations' }) compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" compile('com.github.VictorChow:KotlinAndroidLib:0.1.0') { transitive = false } compile "org.jetbrains.anko:anko-sdk15:0.9.1" // So here it's 15 too compile "org.jetbrains.anko:anko-appcompat-v7:0.9.1" compile "org.jetbrains.anko:anko-design:0.9.1" compile "org.jetbrains.anko:anko-recyclerview-v7:0.9.1" 关于Anko库:Anko是JetBrains开发的一个强大的库。它主要的目的是用来替代以前XML的方式来使用代码生成UI布局:例如Toast:toast(“This is a Toast”)即可
基础语法:
基本语法
数据类型及关键字参见官方文档及资料
val:局部常量:必须在定义的时候赋初始值
var:局部变量
静态变量或常量以及方法:类比java中的static关键字
companion object:伴生对象(或者理解为陪伴类一生的对象?)
在companion object {
//静态变量
private var name: String?= null
//静态方法
fun initName(name1:String?): String?{
this.name = name1
}
}
量的定义:
1.lateinit var name:String(lateinit var: kotlin软关键字:此处表示对变量在后期再进行初始化工作)
2.var name:String = "xxx"(这种定义方式必须赋值,且不能为null)
3.var name:String? = null (类比上一种,可以赋值怒null,注意?这个符号,这是Kotlin语言的一个重要符号,可以理解为:允许为空值,后面还会遇到)
4.val name:String = "xxx"(val关键字修饰的常量,必须在定义时初始化)
方法定义或其他
1.fun method(参数:参数类型):(返回值:返回值类型){方法体}
2.override fun onCreate(参数表){} //override重写关键字,此处即重写onCreate(参数表)方法
3.静态方法:见上文
类型转换
1.toString()等方法
2.as关键字:Java中:textView:TextView = (TextView)findViewbyId(R.id.xxx)
Kotlin中:textView = findViewbyId(R.id.xxx) as TextView
或者有使用Anko:textView = find(R.id.xxx)as TextView
类的继承和接口的实现
A,B为类,C为接口
java:
class A extends B implements C
kotlin:
class A: B , C
构造方法
1.默认构造方法:
class myAdapter(val context: Context,var isCollect:Boolean): RecyclerView.Adapter<MyViewHolder>() {}
主构造方法(或者称为默认构造方法)紧跟在类名后面(参数表中定义的变量或者常量,类似于java中在类中的普通变量或者常量,无需在类中再次定义)。
或者:
class myAdapter: RecyclerView.Adapter<MyViewHolder>() {
//这是第二种写带参主构造方法的写法
constructor(val context: Context) {}
//这是第三种写带参主构造方法的写法
constructor(val context: Context):super(context){}
}
2.次构造方法:
class myAdapter(val context: Context,var isCollect:Boolean): RecyclerView.Adapter<MyViewHolder>() {
lateinit var storiesList: List<ThemesItemsBean.StoriesBean>
constructor(context: Context , themesItemsBean: ThemesItemsBean,isCollect: Boolean) : this(context,isCollect){
storiesList = themesItemsBean.stories
}
次构造方法:使用constructor(参数表):this(默认参数表(必须要有)){方法体}
}
构造方法注意点:constructor软关键字可声明主次构造方法,但是主构造方法不能加(:this(默认参数表),主构造方法可后接:super(参数)),次构造方法不能少(:this(默认参数表))
实际代码中遇到的一些坑
1.添加和切换fragment:
java中:
transaction = FragmentManager.beginTransaction()
再通过这个transaction来实现添加或者隐藏显示fragment
kotlin中:
val transaction: android.support.v4.app.FragmentTransaction = supportFragmentManager.beginTransaction()
注意这里的supportFragmentManager虽然方法中也有提示Fragment,但在实际使用中,会出现参数表不匹配的错误导致无法添加成功
2.Actionbar的使用:
在java中:
setSupportActionBar(ToolBar)
ActionBar actionBar = getSupportActionBar()
actionBar.setTitle("xxxxxx")
在kotlin中:
setSupportActionBar(ToolBar)
supportActionBar?.setTitle("xxxxxx")
3.Anko的find方法在ViewHolder类中使用报错:
貌似是null can not cast to non-null:
原来的使用方法是:
var textView = itemView.find(R.id.xxx) as TextView
解决办法(查找自StackOverFlow):
var textView = itemView.findViewById(R.id.xxx) as? TextView
4.关于List< ClassType>
不同于java,kotlin语言一大优势就在于更好的处理null异常,而这里的List<>,在kotlin中是没有提供诸如add,set等方法的,只是默认提供了get和其他一些只读操作方法
var storiesList:List<StoriesBean> = ArrayList<StoriesBean>()
这种写法无法在代码中使用storiesList.add(xxx)(不会出现add()方法)
//解决办法:
var storiesList:MutableList<StoriesBean> = ArrayList<StoriesBean>()
storiesList.add(xxx)(这样才能正常add或者set)
Kotlin(一)的更多相关文章
- Kotlin的Lambda表达式以及它们怎样简化Android开发(KAD 07)
作者:Antonio Leiva 时间:Jan 5, 2017 原文链接:https://antonioleiva.com/lambdas-kotlin/ 由于Lambda表达式允许更简单的方式建模式 ...
- 用Kotlin实现Android定制视图(KAD 06)
作者:Antonio Leiva 时间:Dec 27, 2016 原文链接:https://antonioleiva.com/custom-views-android-kotlin/ 在我们阅读有关c ...
- Kotlin与Android SDK 集成(KAD 05)
作者:Antonio Leiva 时间:Dec 19, 2016 原文链接:https://antonioleiva.com/kotlin-integrations-android-sdk/ 使用Ko ...
- Kotlin的android扩展:对findViewById说再见(KAD 04)
作者:Antonio Leiva 时间:Dec 12, 2016 原文链接:http://antonioleiva.com/kotlin-android-extensions/ 你也许已厌倦日复一日使 ...
- Kotlin类:功能更强、而更简洁(KAD 03)
作者:Antonio Leiva 时间:Dec 7, 2016 原文链接:http://antonioleiva.com/classes-kotlin/ Kotlin类尽可能简单,这样用较少的代码完成 ...
- Kotlin中变量不同于Java: var 对val(KAD 02)
原文标题:Variables in Kotlin, differences with Java. var vs val (KAD 02) 作者:Antonio Leiva 时间:Nov 28, 201 ...
- 用Kotlin创建第一个Android项目(KAD 01)
原文标题:Create your first Android project using Kotlin (KAD 01) 作者:Antonio Leiva 时间:Nov 21, 2016 原文链接:h ...
- Android的Kotlin秘方(II):RecyclerView 和 DiffUtil
作者:Antonio Leiva 时间:Sep 12, 2016 原文链接:http://antonioleiva.com/recyclerview-diffutil-kotlin/ 如你所知,在[支 ...
- Android的Kotlin秘方(I):OnGlobalLayoutListener
春节后,又重新“开张”.各位高手请继续支持.谢谢! 原文标题:Kotlin recipes for Android (I): OnGlobalLayoutListener 原文链接:http://an ...
- KOTLIN开发语言文档(官方文档) -- 2.基本概念
网页链接:https://kotlinlang.org/docs/reference/basic-types.html 2. 基本概念 2.1. 基本类型 从可以在任何变量处理调用成员函数和属性 ...
随机推荐
- python数据结构-如何快速找到多个字典中的公共键
如何快速找到多个字典中的公共键 问题举例 统计每轮都进球的球员: 第1轮{‘tom’:1, 'meixi':2} 第2轮{‘coco’:3, 'meixi':4, 'marton':2} 第3轮{'c ...
- vue2Leaflet使用 Vue2Leaflet-master 的demo
首先下载该demo 地址:https://github.com/KoRiGaN/Vue2Leaflet 下载后可以运行里面的例子,在examples文件夹内,该文件夹本身就是一个完整的项目 然后cmd ...
- Ubuntu16.04彻底卸载MySQL
删除mysql的数据文件 sudo rm /var/lib/mysql/ -R 删除mysql的配置文件 sudo rm /etc/mysql/ -R 自动卸载mysql(包括server和clien ...
- ZooKeeper是什么
ZooKeeper概念 相信大家对 ZooKeeper 应该不算陌生,但是你真的了解 ZooKeeper 是什么吗?如果别人/面试官让你讲讲 ZooKeeper 是什么,你能回答到哪个地步呢? 本人曾 ...
- Java中try、finally语句中有return时的执行情况 [转]
原文:http://kingj.iteye.com/blog/1436761 在Java中当try.finally语句中包含return语句时,执行情况到底是怎样的,finally中的代码是否执行,大 ...
- SiteCore Experience Analytics-路径分析地图
路径分析地图 路径分析器是一个应用程序,允许您查看联系人在浏览网站时所采用的各种路径.您可以查看联系人在转换目标并与广告系列互动时所采用的路径,让您深入了解哪些路径为每次转化提供最佳参与价值,以及哪些 ...
- Array类型和方法
var ft = new Array(); var ft = new Array(20); var ft = new Array("1","2","3 ...
- week_one-python基础 基本语法、流程控制
金角大王的紫金葫芦,python开发环境介绍链接:http://list.youku.com/albumlist/show/id_28961509.html # Author:larlly pytho ...
- SQL语句汇总——数据修改、数据查询
首先创建一张表如下,创建表的方法在上篇介绍过了,这里就不再赘述. 添加新数据: INSERT INTO <表名> (<列名列表>) VALUES (<值列表>) ...
- [c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset
一.memset #include<stdio.h> #include<stdlib.h> #include<memory.h> void *mymemset(vo ...