以下是一个Kotlin版Android程序的小例子
先在build.gradle文件中添加布局拓展,可以在java文件中直接使用在布局文件中声明的id的控件,省去findViewById的步骤。
apply plugin: 'kotlin-android-extensions'
再在build.gradle中的dependencies中添加anko,可以直接在java文件中写布局,省去xml文件。
compile "org.jetbrains.anko:anko-sdk15:0.9.1"
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="linchange.example.com.kotlin_android2.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
android:gravity="center" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
package linchange.example.com.kotlin_android2
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import org.jetbrains.anko.onClick
import org.jetbrains.anko.startActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
fab.onClick { view -> //为浮动按钮设置点击事件
Snackbar.make(view!!, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
tv.text = "${System.currentTimeMillis()}" //设置textView的文本内容为当前时间
//跳转页面,泛型为需要跳转的页面,圆括号中的键值对为所需放入的Extras
startActivity<Main2Activity>("Key" to "from The Great Wall")
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == R.id.action_settings) {
return true
}
return super.onOptionsItemSelected(item)
}
}
Main2Activity.java
package linchange.example.com.kotlin_android2
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.Editable
import android.text.TextWatcher
import android.view.Gravity
import android.widget.Toast
import org.jetbrains.anko.relativeLayout
import org.jetbrains.anko.textView
import org.jetbrains.anko.wrapContent
class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
relativeLayout { //相对布局
textView(intent.extras["Key"]?.toString() ?: "Nothing") { //设置textView的文字
addTextChangedListener(object : TextWatcher { //匿名对象调用接口
override fun afterTextChanged(s: Editable?) {
Toast.makeText(this@Main2Activity, "textView Change",
Toast.LENGTH_SHORT).show()
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
}.lparams(wrapContent, wrapContent) { //设置textView的布局参数
gravity = Gravity.CENTER
}
}
}
}