首页 > 其他分享 >Android Kotlin MVP 登录实现

Android Kotlin MVP 登录实现

时间:2023-06-21 18:44:54浏览次数:33  
标签:MVP String Kotlin app fun Android com example Unit

一:新建MVP软件包文件

 activity_main.xml 界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginStart="40dp"
        android:layout_marginEnd="30dp"
        >

        <ImageView
            android:layout_width="150dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_launcher_foreground"
            android:layout_gravity="center"
            />

        <EditText
            android:id="@+id/countId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入账号"
            android:textSize="15dp"
            />
        <EditText
            android:id="@+id/pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码"
            android:inputType="textPassword"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            />

        <Button
            android:id="@+id/login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="登录"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            />



    </LinearLayout>

</RelativeLayout>

效果

 

view Binding (视图绑定)

        视图绑定是一项功能,可让你更轻松地编写与视图交互的代码。在模块中启用视图绑定后,它会为该模块中存在的每个 XML 布局文件生成一个 绑定类。绑定类的实例包含对在相应布局中具有 ID 的所有视图的直接引用。

buildFeatures {
viewBinding true
}

依赖项

    implementation 'androidx.databinding:databinding-runtime:4.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation "androidx.viewpager2:viewpager2:1.0.0"

view

view 是Activity实现  这是实现接口
package com.example.app.view


interface ILoginView {
    fun getUserName() : String
    fun getPassword() : String
    fun showToast(msg : String) : Unit
    fun gotoLoginSuccessActivity(msg:String) : Unit
}
presenter
Presenter主要作为沟通View和Model的桥梁,它从Model层检索数据后,返回给View层,但是不想典型的MVC结构,因为它也可以决定与View层的交互操作。
LoginPresenter 数据检索
package com.example.app.presenter
import android.content.Intent
import android.widget.Toast
import com.example.app.model.ILoginModel
import com.example.app.view.ILoginView


class LoginPresenter(var mView : ILoginView, var mModel : ILoginModel?) : ILoginPresenter , ILoginListener{

    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("请输入账户名称")
    }

    override fun onPasswordError() {
        mView!!.showToast("请输入账户密码")
    }

    override fun onSuccess() {
        mView!!.gotoLoginSuccessActivity("登录成功")

    }

    override fun one rror() {
        mView!!.showToast("登录失败")
    }
}
ILoginPresenter 接口定义
package com.example.app.presenter


interface ILoginPresenter {
    fun login() : Unit
}
ILoginListener 
桥接监听
package com.example.app.presenter


interface ILoginListener {
    fun onUserNameError() : Unit
    fun onPasswordError() : Unit
    fun onSuccess() : Unit
    fun one rror() : Unit
}
Model 
Model 数据源数据接口

LoginModel 登录接口业务实现
package com.example.app.model


import com.example.app.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) {
        userName ?: run { onLoginListener.onUserNameError(); return }
        password ?: run { onLoginListener.onPasswordError(); return }

        onLoginListener.onSuccess()




    }
}
ILoginModel 定义接口 桥接presenter
package com.example.app.model


import com.example.app.presenter.ILoginListener

interface ILoginModel {
    fun login(userName : String,password : String,onLoginListener: ILoginListener) : Unit
}

 目录

 

标签:MVP,String,Kotlin,app,fun,Android,com,example,Unit
From: https://www.cnblogs.com/jstblog/p/17496879.html

相关文章

  • 【车载开发】Android Automotive车载操作系统开发解密篇
    提到Android车载,我们应该都不陌生。传统的车载功能单一,无太多娱乐性,而随着智能化时代的发展,车载系统也被赋予了在系统中预装Android应用的能力,基于Android平台的车载信息娱乐系统——AndroidAutomotive应运而生。而今,包括BAT在内许多传统互联网企业纷纷布局手机产业,追逐新能源造......
  • 入一线大厂;Android程序员一定要做的事
    前言很多做Android开发的朋友们都知道,从15年开始,就不断的有人在唱衰Android,在某乎上,经常可以看到这种标题。如果没有一点迹象也就不会有这么多风声传出了,之前在某呼上看到有人说是因为15年培训班兴起,线下培训机构陆续开发线上课程。培训机构造就了一大批速成的初级Android开发进入......
  • Android开发优化的几点建议
    前言安卓开发大军浩浩荡荡,经过近十年的发展,Android技术优化日异月新,Android系统性能也已经非常流畅,可以在体验上完全媲美iOS。但是,到了各大厂商手里,改源码、自定义系统,使得Android原生系统变得鱼龙混杂,然后到了不同层次的开发工程师手里,因为技术水平的参差不齐,即使很多手机在跑分......
  • Android Bresenham 直线算法 让你的手势更丝滑
    Bresenham算法是一种用于绘制直线的算法,它通过在离散的像素点上进行逐步的迭代来绘制出近似直线。以下是一个示例代码,演示了如何使用Bresenham算法绘制直线:fundrawLine(x0:Int,y0:Int,x1:Int,y1:Int){valdx=Math.abs(x1-x0)valdy=Math.abs(y1-......
  • app直播源代码,Android中点击图片放大的简单方法
    app直播源代码,Android中点击图片放大的简单方法Java代码: publicvoidonThumbnailClick(Viewv){//finalAlertDialogdialog=newAlertDialog.Builder(this).create();//ImageViewimgView=getView();//dialog.setView(imgView);//dialog.show();  //全屏显示的......
  • 索然无味!Kotlin开发从入门到上天,一篇文章就搞定了!(万字长文)
    标题党?看起来可能有点标题党的意思,但我知道,不这样,你们可能看不到这篇。关于Kotlin相关记录,如果有意查看我的github,其超10w字(其中8w是代码吗,哈哈)。。为什么要学?在不牺牲性能或安全性的前提下,许多的Kotlin功能使代码比Java更加简洁易懂。Kotlin编译为字节码,因此其性能与Java一样好......
  • 百度内网《Android车载操作系统开发指南》惨遭泄漏,24小时删!!!
    软件定义汽车背景下,操作系统是汽车生态发展的灵魂。随着汽车电动化、智能化、网联化的发展,汽车操作系统已经成为车辆中重要的组成部分之一,一定程度上决定了车辆的安全性、舒适度、智能化水平和整体性能。而Android系统开源、免费应用资源多、应用UI/Lunch、操控等人机交互开发,易定......
  • 全网最全Android Framework面试汇总(内含Binder、Hinder、 AMS、WMS等核心技术)
    在如今的招聘市场中,学习Framework已经成为了一个非常重要的竞争力。许多面试官都会问到Framework相关的问题:进程通信你用过哪些?原理是什么?ThreadLocal的原理,以及在Looper是如何应用的?Handler可能会导致内存泄露,为什么?Binder机制是如何跨进程的?…同时AndroidFramework作为......
  • Android车载应用系统开发入门教程(非常详细)从零基础入门到精通,看完这一篇就够了
    前言众所周知,国内的安卓市场内卷极其严重,原生应用开发可谓是寸步难行,但整个Android生态却无比繁荣,手机、平板、电视、音视频等等,特别是在智能汽车+电动汽车的浪潮下,车载行业成为许多Android人竞相奔走的行业,薪资待遇以及岗位招聘呈上涨趋势。对于涉事多年的「老Android人」来说,转行......
  • 2023年Android社招面试题集锦(最近准备面试的可以看看~)
    最近有不少小伙伴咨询社招,春招的事情,小编这里收纳了一篇《如何找到一份实习工作》的内容,作者是阿木(一家知名的互联网大厂),这篇内容算是他对自己找工作经历的一个总结吧,对于社招、在校生,尤其是想找实习的小伙伴会很有帮助,同时还有最新面试题汇总。顺带给大家同步一个关键的信息,暑期......