Android Kotlin MVP 模式实现登入注册

本文介绍了如何在Android项目中使用Kotlin实现一个登录功能,包括MainActivity中的逻辑处理、视图接口定义、业务逻辑模型以及Presenter的角色。用户输入验证和登录成功的跳转都在示例中详细展示。
摘要由CSDN通过智能技术生成

  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>

最后附上源码下载链接,免费下载

 源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值