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. 基本类型 从可以在任何变量处理调用成员函数和属性 ...
随机推荐
- redis示例 - 限速器,计时器
INCR INCR key 将 key 中储存的数字值增一. 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作. 如果值包含错误的类型,或字符串类型的值不能表示 ...
- HTML5 元素属性介绍
HTMLElement 表示所有的 HTML 元素. 这里将以事件属性和非事件属性的分类进行介绍. 事件属性大多继承自GlobalEventHandlers,非事件属性大多继承自Element. 菜单 ...
- Gis数据处理2 ---8.18
1空间参考: 了解大地水准面,参考椭球体,基准面的概念 以及之间的关系 基准面描述的是参考椭球体中心 跟地心的关系 我们常说的北京54.西安80.CGCS2000,实际上指的是我国的三个大地基 ...
- 7.mongo python 库 pymongo的安装
1.Python 中如果想要和 MongoDB 进行交互就需要借助于 PyMongo 库,在CMD中使用命令即可[注意此处是pip3,pip无法安装]: pip3 install pymongo 2. ...
- python split()函数的用法
转自: https://blog.csdn.net/orangefly0214/article/details/80810449 函数:split() Python中有split()和os.path. ...
- mybatis的sql映射文件不能打包进目录解决办法
方法二: <build> <finalName>qwe</finalName> <plugins> <plugin> <groupId ...
- APScheduler(Python化的Cron)使用总结 定时任务
APScheduler(Python化的Cron)使用总结 简介 APScheduler全程为Advanced Python Scheduler,是一款轻量级的Python任务调度框架.它允许你像Cr ...
- Generative Adversarial Nets
1. 基本思想 两个模型: 判别器:预测从生成器生成的样本的概率 生成器:生成样本时,最大化使得判别器发生错误的概率 最后得到唯一解:使得生成器生成的样本输入到判别器中,得到的概率全是1/2. ...
- lvs 初始 第一章
Linux Virtual Server 第一章 初识 一 . 介绍 LVS集群采用IP负载均衡技术和基于内容请求分发技术.调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行,且调度器自动 ...
- 如何退出vim
按ESC键 按ESC键 按ESC键 然后: 最下面出现一条能输入命令的地方 输入冒号 输入冒号 输入冒号 然后输入命令: :w 保存文件但不退出 :w file 将修改另外保存到 file 中,不退出 ...