转载请注明出处
1, 什么是kotlin
Kotlin是一门基于JVM的编程语言,它正成长为Android开发中用于替代Java语言的继承者。Java是世界上使用最多的编程语言之一,当其他编程语言为更加便于开发者使用而不断进化时,Java并没有像预期那样及时跟进。
使用Kotlin,你可以很容易的在Android工程中替代Java或者与Java混合使用。
2, AS 中kotlin 的配置
1,首先安装kotlin插件
2,项目的Project 的build.gradle中添加kotlin 的版本号
// Top-level build file where you can add configuration options common to all sub-projects/modules.
ext.kotlin_version = "1.0.3"
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://jitpack.io"
}
}
}
在module中的build.gradle
再buildscript中
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
在dependencies中添加kotlin依赖
//kotlin
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
然后添加对kotlin 的支持
//kotlin
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
3,使用kotlin
3,Kotlin Demo
通过使用Kotlin+RecyclerView 来展示一组数据
首先创建基类BaseActivity
/**
* Created by huanjulu on 16/8/19.
*/
abstract class BaseActivity : AppCompatActivity() {
abstract val mContentLayoutResouredId: Int
val toolbar: Toolbar by lazy { find<Toolbar>(R.id.toolbar) }//Kotlin 中的代理
companion object { //这部分代码你可以理解为有点类似JAVA中的Static,是可以直接通过类名调用
}
override fun onCreate(savedInstanceState: Bundle?) {
super<AppCompatActivity>.onCreate(savedInstanceState)
if (mContentLayoutResouredId != 0) setContentView(mContentLayoutResouredId) else creatContentView()
if (savedInstanceState != null) handleSavedInstanceState(savedInstanceState)
if (intent.extras != null) handleIntentBundleExtrax(intent.extras)
ButterKnife.bind(this)
setSupportActionBar(toolbar)
setUp()
}
override fun onResume() {
super<AppCompatActivity>.onResume()
}
override fun onPause() {
super<AppCompatActivity>.onPause()
}
override fun onDestroy() {
super<AppCompatActivity>.onDestroy()
}
open fun creatContentView(): View {
throw UnsupportedOperationException("sorry , your layoutviewId ==0 , createContentView() must be implemented by subclasS")
}
open fun handleIntentBundleExtrax(bundle: Bundle) {
}
open fun handleSavedInstanceState(savedInstanceState: Bundle?) {
}
open fun setUp() {
}
open fun handleIntentExtrax(key: Any): String? {
if (key is String) return intent.getStringExtra(key)
return null
}
}
然后看下主类MainActivity :
/**
* Created by huanjulu on 16/8/25.
*/
class MainActivity : BaseActivity() {
override val mContentLayoutResouredId: Int = R.layout.activity_main
val context = this
val list = mutableListOf("Android", "Binder", "Asynctask", "OOM")
override fun setUp() {
super.setUp()
var recyView = findViewById(R.id.id_recyclerview) as RecyclerView
var mHotNewsDataAdapter = MainAdapter(LayoutInflater.from(this))
recyView!!.layoutManager = LinearLayoutManager(context)
recyView!!.adapter = mHotNewsDataAdapter
}
}
说明:
1 :关于继承
- **在Kotin 中所有的类都有共同的父类 Any
- **声明一个明确的父类,需要在类头后加冒号再加父类
2:关于属性和变量
- **在 Kotlin 中类可以有属性,我们可以使用 var 关键字声明可变属性,或者用 val 关键字声明只读属性(有点类似java中的final修饰符)
- **通过名字直接使用一个属性
例如:val user=User()
user.name
user.age 等等
执行代码
更多的关于kotlin 的学习资料