1、新建一个工程,在android3.0可以直接选择“include Kotlin support”。如果不是3.0及以上也没关系,在后面手动安装一个Kotlin
插件即可。
2、安装Kotlin 插件。点击File->Setting->Plugins->browse repositories..->在左上角的输入框搜索Kotlin 。我已经安装过所以卡看不见绿色的install按钮。记住安装的Kotlin的版本号我的是V1.3.10,记不住代会知道怎么查就好啦。
3、由于我创建的是java文件,需要转换成Kotlin文件,android studio支持自动转换。
通过菜单栏依次调出 Code ->Convert Java File to Kotlin File.。
转化后的文件如下,上面哟一个提示,点击蓝色的“configure”
弹出一个框,需要选择 和安装的Kotlin一样的版本v1.3.10,但是这里最高只有1.2.71没关系,我们代会手动修改成1.3.10就可以
点击ok之后将ext.kotlin_version='version'改成1.3.10。也就是kotlin插件的版本号。点击“sync"就好了
配置完成以后就开始第一个登录程序了。kotlin结合anko构建布局比之前用xml文件创建布局要快,但是这是我们第一次试用kotlin写就还有沿用xml构建布局的方式写。不知道的可以搜一下kotlin如何结合anko构建布局。
登录界面activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#ffffff"
android:orientation="vertical"
android:padding="40dp">
<LinearLayout
android:id="@+id/lin_count"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginTop="45dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="15dp"
android:src="@drawable/ic_launcher_background" />
<EditText
android:id="@+id/loginAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="登录账户"
android:maxLength="11"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/lin_password"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="15dp"
android:src="@drawable/ic_launcher_background"/>
<EditText
android:id="@+id/loginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="账户密码"
android:password="true"
android:maxLength="11"
android:textSize="16sp"/>
</LinearLayout>
<Button
android:id="@+id/login_button"
android:layout_width="300dp"
android:layout_height="44dp"
android:layout_gravity="center"
android:layout_marginTop="18dp"
android:text="登录"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>
mainActivity.java
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import android.view.View
//kotlin支持直接使用Id的操作控件,不需要findViewById,要使用id操作控件,需要加上下面这句话。
import kotlinx.android.synthetic.main.activity_main.*
/**
* Created by 林亮
*/
//kotlin用冒号:表示继承
class MainActivity : Activity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//直接使用id操作
login_button.setOnClickListener(this)
rem_pas_check.setOnClickListener(this)
newUser.setOnClickListener(this)
}
override fun onClick(view: View?) {
//when取代switch,比switch更加强大
when (view) {
login_button -> {
if (LegalUserInfo()==true) {
//Toast.makeText(this, "合法用户", Toast.LENGTH_LONG).show();
var intent: Intent = Intent()
//跳转
intent.setClass(this, SecondActivity::class.java)
startActivity(intent)
} else {
Toast.makeText(this, "重新输入", Toast.LENGTH_LONG).show();
}
}
rem_pas_check -> {
Toast.makeText(this, "3333", Toast.LENGTH_LONG).show();
Log.i("test==", "rem_pas_check");
println("")
}
newUser -> {
Toast.makeText(this, "444", Toast.LENGTH_LONG).show();
Log.i("test==", "newUser");
}
}
}
fun LegalUserInfo(): Boolean {
//获取editview的值
var userName: String? = loginAccount.getText().toString().trim();
var password: String? = loginPassword.getText().toString().trim();
if (userName == null || "".equals(userName)) {
Log.i("test==LegalUserInfo", "userName>>>");
return false
} else if (password == null || "".equals(password)) {
Log.i("test==LegalUserInfo", "password>>>");
return false
}
return true
}
}
上面的代码很简单,看几眼就能懂,如果对于语法不熟。可以先学一学语法