2019.02.12 11:33
链接:
kotlin语言中文
https://www.kotlincn.net/
kotlin教程
https://www.runoob.com/kotlin/kotlin-tutorial.html
使用kotlin高效地开发android
https://www.jianshu.com/p/5f77209abb9b
Android快速转战Kotlin教程
https://blog.youkuaiyun.com/github_33304260/article/details/80343514
对比java kotlin
https://github.com/MindorksOpenSource/from-java-to-kotlin
配置环境
https://www.jianshu.com/p/8aa7b407d24f
技术博客
https://antonioleiva.com/
https://legacy.gitbook.com/book/wangjiegulu/kotlin-for-android-developers-zh/details
对应代码
https://github.com/antoniolg/Kotlin-for-Android-Developers
1、布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/forecast_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
2、创建adapter:
package cn.nubia.weatherkotlin.adapter
import android.support.v7.widget.AppCompatAutoCompleteTextView
import android.support.v7.widget.RecyclerView
import android.view.ViewGroup
import android.view.ViewParent
import android.widget.TextView
class ForecastListAdapter(val items: List<String>) : RecyclerView.Adapter<ForecastListAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(TextView(parent.context))
}
override fun getItemCount(): Int {
return items.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.textView.text = items.get(position)
}
class ViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
}
3、MainActivity.kt
val forecastList = findViewById(R.id.forecast_list) as RecyclerView
forecastList.layoutManager = LinearLayoutManager(this)
forecastList.adapter = ForecastListAdapter(items)
4、toast函数
fun Context.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, message, duration).show()
}
5、网络请求
public class Request(val url: String) {
public fun run() {
val forecastJsonStr = URL(url).readText()
Log.e(javaClass.simpleName, forecastJsonStr)
}
}
6、权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
7、数据类
data class Forecast(val date: Date, val temperature: Float, val details: String)
8、