在Android studio中使用Kotlin
第一步:我们首先应该将kotlin的插件导入Android Studio,如果你用的是Android Studio3.0或更高版本这一步你可以跳过
第二步:我们需要在gradle中添加以下代码(下面是我gradle的配置):
buildscript {
ext.kotlin_version = '1.0.0'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
--------------------------------------------------------------------------------
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
android {
compileSdkVersion 25
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.zl.sensible"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.squareup.okhttp3:okhttp:3.2.0'
}
大家应该知道这两部分往哪里添加吧?
有了以上两步大家就可以在Android Studio中愉快的编写Kotlin代码了。
Kotlin在Android开发中的使用小技巧
使用了Kotlin后从此无需再findViewById了,kotlin支持通过View的id来之间操作,代码如下:
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zl.sensible.MainActivity">
<EditText
android:id="@+id/main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
Activity中的代码
main_text.hint = "张磊"
main_button.text = "提交"
main_text.setOnClickListener { v -> Toast.makeText(this, "给个提示", Toast.LENGTH_SHORT).show() }
main_button.setOnClickListener { v -> Toast.makeText(this, "给个提示", Toast.LENGTH_SHORT).show() }
是不是很好用呀,从此不再需要findViewById了。
我在使用Kotlin时遇到的问题
其实Kotlin的使用我也是一知半解(只懂基本语法),这不就出问题了吗,相信大家在用java写代码的使用会经常用到接口回调(这个很普遍吧),但是Kotlin怎么使用呢我就不太了解了。
但是最近我终于知道怎么写了。
如果你要回调的接口里面只有一个方法,那你就不需要去写一个接口了,Kotlin有更好的方法来书写,代码如下:
//声明接口
var openListener: ((position: Int, data: String) -> Unit)? = null
//为接口赋值
fun setOnOpenListener(openListener: ((position: Int, data: String) -> Unit)?) {
this.openListener = openListener
}
//在用到的地方进行调用
mAdapter.setOnOpenListener { position, data -> Log.e("logs== ", "我被调用了")}
如果你的借口有多个方法,那么你就得乖乖的去创建一个接口了,代码如下:
1.创建接口
interface Callback {
fun onSuccess() {
}
fun onError() {
}
}
2.声明接口并赋值
//声明接口
private var callback: Callback? = null
//接口赋值
fun setCallback(callback: Callback) {
this.callback = callback
}
3.在用到的地方调用
//赋值调用
mAdapter.setCallback(object : Callback {
override fun onSuccess() {
super.onSuccess()
}
override fun onError() {
super.onError()
}
})
大功告成,可以愉快的去封装网络请求了。
我是今年的六月接触的Kotlin,学了两三天就被卡在了接口回调这里,后来因为工作的原因就没在学习了,时隔两个月后我又有时间来学了,功夫不负有心人呀终于知道怎么写了,好开森呀。