Kotlin google已经发布很久了,但是一直没有在项目中实际应用,今天正好有空了解学习一下,并记录过程,方便后续回顾,代码也是网上找的,自己稍微做了一些修改,如有问题多多指教
先上效果图
login
目录接口如下
1、登入直接在MainActivity中写
package com.example.kotlintest
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.kotlintest.model.LoginModel
import com.example.kotlintest.presenter.LoginPresenter
import com.example.kotlintest.view.ILoginView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() , ILoginView {
var mPresenter : LoginPresenter = LoginPresenter(this@MainActivity, LoginModel.newInstance())
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//mUI.setContentView(this@MainActivity)
setContentView(R.layout.activity_main)
login();
}
override fun getUserName(): String {
return countId.text.toString()
}
override fun getPassword(): String {
return pwd.text.toString()
}
override fun showToast(msg: String) {
//toast(msg)
Toast.makeText(this,msg,Toast.LENGTH_LONG).show()
}
private fun login() {
login.setOnClickListener {
mPresenter.login()
}
}
override fun gotoLoginSuccessActivity() {
startActivity(Intent(this@MainActivity,LoginSuccessActivity::class.java))
}
}
view接口定义
package com.example.kotlintest.view
interface ILoginView {
fun getUserName() : String
fun getPassword() : String
fun showToast(msg : String) : Unit
fun gotoLoginSuccessActivity() : Unit
}
登入成功
package com.example.kotlintest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class LoginSuccessActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login_success)
}
}
2、Model 是业务逻辑处理层,处理登入逻辑接口实现
package com.example.kotlintest.model
import com.example.kotlintest.presenter.ILoginListener
interface ILoginModel {
fun login(userName : String,password : String,onLoginListener: ILoginListener) : Unit
}
package com.example.kotlintest.model
import com.example.kotlintest.presenter.ILoginListener
class LoginModel : ILoginModel {
companion object{
private var mLoginModel : LoginModel ?= null
fun newInstance() : LoginModel?{
if (mLoginModel == null) mLoginModel = LoginModel()
return mLoginModel
}
}
override fun login(userName: String, password: String, onLoginListener: ILoginListener) {
if ("zhangsan" != userName) {
onLoginListener.onUserNameError()
return
}
if ("123" != password) {
onLoginListener.onPasswordError()
return
}
onLoginListener.onSuccess()
}
}
3、Presenter 负责完成View于Model间的交互
package com.example.kotlintest.presenter
interface ILoginListener {
fun onUserNameError() : Unit
fun onPasswordError() : Unit
fun onSuccess() : Unit
fun onError() : Unit
}
package com.example.kotlintest.presenter
interface ILoginPresenter {
fun login() : Unit
}
package com.example.kotlintest.presenter
import com.example.kotlintest.model.ILoginModel
import com.example.kotlintest.view.ILoginView
class LoginPresenter(var mView : ILoginView, var mModel : ILoginModel?) : ILoginPresenter , ILoginListener{
// var mView : ILoginView ?= null
// var mModel : ILoginModel ?= LoginModel()
override fun login() {
var userName : String = mView!!.getUserName()
var password : String = mView!!.getPassword()
this.mModel!!.login(userName,password,this@LoginPresenter)
}
override fun onUserNameError() {
mView!!.showToast("onUserNameError--->")
}
override fun onPasswordError() {
mView!!.showToast("onPasswordError--->")
}
override fun onSuccess() {
mView!!.showToast("onSuccess--->")
mView!!.gotoLoginSuccessActivity()
}
override fun onError() {
mView!!.showToast("onError--->")
}
}
4、xml布局文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:layout_gravity="center"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="20dp"
>
<EditText
android:id="@+id/countId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:hint="账户"
>
</EditText>
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
>
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="登入"
/>
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="注册"
/>
</LinearLayout>
</LinearLayout>
activity_login_success.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
tools:context=".LoginSuccessActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login success"
/>
</LinearLayout>
最后附上源码下载链接,免费下载