下面就为大家介绍怎么使用Kotlin来开发Android
上面这篇中我们在下载Kotlin插件的时候也下了一个功能扩张插件,其实这个插件大有用处,它可以使得我们在不使用注解和第三方库的情况下不使用findViewById来实例化控件,我们只需要导入对应的布局然后直接使用id来使用这个View就可以了。
扩展功能插件的使用
要使用这个功能:
1、首先需要导入这个插件的依赖,在Module的gradle中:
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
2、建立一个layout布局,并给View定义一个id:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="56dp"
android:textSize="16sp"/>
</LinearLayout>
3、导入布局,直接使用id来使用这个View:
在Activity中需要这样导入对应的layout
import kotlinx.android.synthetic.activity_main.*
然后就可以直接使用id来使用这个View了
textView.setText("Hello Kotlin")
Kotlin开发Andorid程序示例
activity_main.xml
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="56dp"
android:textSize="16sp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MainActivity.kt
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import kotlinx.android.synthetic.activity_main.*
import java.util.*
public class MainActivity : AppCompatActivity() {
private var mRecyclerView: RecyclerView? = null
private var datas: MutableList<String>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getData()
textView.setText("Hello Kotlin")
mRecyclerView = findViewById(R.id.recyclerView) as RecyclerView
mRecyclerView!!.setHasFixedSize(true)
mRecyclerView!!.setLayoutManager(LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false))
var adapter:RecyclerAdapter = RecyclerAdapter(datas)
mRecyclerView!!.setAdapter(adapter)
}
public fun getData():Unit{
datas = ArrayList<String>()
for(i in 1..100){
datas!!.add(i.toString())
}
}
}
RecyclerAdapter.kt
public class RecyclerAdapter(val datas: MutableList<String>?) : RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): RecyclerViewHolder {
val view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item, viewGroup, false)
val holder = RecyclerViewHolder(view)
return holder
}
override fun onBindViewHolder(recyclerViewHolder: RecyclerViewHolder, i: Int) {
recyclerViewHolder.mTextView.setText(datas!!.get(i))
}
override fun getItemCount(): Int {
return datas!!.size()
}
public class RecyclerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
public var mTextView: TextView
init {
mTextView = itemView.findViewById(R.id.textView) as TextView
}
}
}
运行效果: