kotlin实现登录逻辑

布局文件

<?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"
    android:gravity="center_horizontal"
  >

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/logo"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:gravity="center_horizontal"
        android:id="@+id/relativeLayout">


        <android.support.design.widget.TextInputLayout
            android:id="@+id/username_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/login_username"
                android:maxLines="1"/>

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/password_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/username_layout">

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/login_password"
                android:imeActionLabel="Sign in"
                android:imeOptions="actionUnspecified"

                android:maxLines="1"/>

        </android.support.design.widget.TextInputLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/password_layout"
            android:layout_marginTop="16dp"
            android:orientation="horizontal">

            <Button
                android:id="@+id/login"
                style="?android:textAppearanceSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/login_login"
                android:textStyle="bold"/>

            <Button
                android:id="@+id/register"
                style="?android:textAppearanceSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/login_register"
                android:textStyle="bold"/>

        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

登录 代码 :


class loginActivity : BaseActivity(), LoginView, View.OnClickListener {


    var loginPresenter: LoginPresenter? = null
    var dialog: SweetAlertDialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        loginPresenter = LoginPresenterImpl(this)
        login.setOnClickListener(this)
        register.setOnClickListener(this)
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.login ->
                if (checkContent(true)) {
                    dialog = SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE)
                            .setTitleText("正在登录...")
                    dialog?.setCancelable(false)
                    dialog?.show()
                    loginPresenter?.login(username.text.toString(), password.text.toString())
                }
            R.id.register ->
                Register()
        }
    }

    private fun Register() {

        val intent = Intent(this, RegisterActivity().javaClass)
        startActivity(intent)
    }


    /**
     * 判断
     */
    private fun checkContent(login: Boolean): Boolean {
        username.error = null
        password.error = null

        var cancel = false
        var focusView: View? = null

        if (TextUtils.isEmpty(password.text.toString())) {
            password.error = "密码不能为空"
            focusView = password
            cancel = true
        } else if (password.text.length < 6) {
            password.error = "密码长度不能小于6位"
            focusView = password
            cancel = true
        }

        if (TextUtils.isEmpty(username.text.toString())) {
            username.error = "用户名不能为空"
            focusView = username
            cancel = true
        }

        if (cancel) {
            // There was an error; don't attempt login and focus the first
            // form field with an error.
            if (focusView != null) {
                focusView.requestFocus()
            }
        } else {
            return true
        }
        return false
    }


    override fun loginSuccess(result: LoginResponse) {
        dialog?.changeAlertType(SweetAlertDialog.SUCCESS_TYPE)
        //        dialog?.titleText = result.msg
        dialog?.titleText = "登录成功"
        PreferenceUtil.getInstance(this).setLog(true)
        PreferenceUtil.getInstance(this).setString(PreferenceUtil.USER_NAME, username.text.toString())
        PreferenceUtil.getInstance(this).setString(PreferenceUtil.USER_PWD, password.text.toString())

        val intent = Intent(this, MainActivity().javaClass)
        startActivity(intent)
        dialog?.dismiss()
        finish()
    }

    override fun loginFailed(message: String?) {
        dialog?.changeAlertType(SweetAlertDialog.ERROR_TYPE)
        dialog?.titleText = message
    }

    override fun registerSuccess(result: RegisterResponse) {
        dialog?.changeAlertType(SweetAlertDialog.SUCCESS_TYPE)
        dialog?.titleText = result.msg
    }

    override fun registerFailed(message: String?) {
        dialog?.changeAlertType(SweetAlertDialog.ERROR_TYPE)
        dialog?.titleText = message
    }
}

由于涉及项目隐私 只贴部分代码
项目效果展示apk : http://imtt.dd.qq.com/16891/670C11ECFEA442BE99BACA612A7EA798.apk?fsname=com.ycjt.ycnyzx_1.0.1_1.apk&csr=1bbd

或 : http://android.myapp.com/myapp/detail.htm?apkName=com.ycjt.ycnyzx

安卓开发交流群 : 595856941

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值