智能管家App kotlin版(3)——用户注册/登录/忘记重置密码/个人数据编辑开发

前言:Bmob后端云为我们用户操作带来了极大的便捷,本文章主要是涵盖了一个用户系统逻辑,实现用户的登录,注册,找回密码,修改密码,邮箱验证,以及记住密码等功能的实现,通过本章你可以学习到宝贵的用户操作逻辑,同时可以学习到自定义的Dialog以及头像的选择和裁剪!

此篇文章紧做关于该项目的用户注册/登录/忘记重置密码开发,后续功能实现请关注后续文章!!!

此文章实现功能将过程中,分布展示!!!

一.用户管理—Bmob简单集成

这地方我并没有什么自己的见解,只是按照官方文档,一步步操作罢了

官方文档

二.用户管理—用户注册功能开发

功能截图:
在这里插入图片描述
1.在ui包下,创建RegisteredActivity,编写注册界面交互界面:

<?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:orientation="vertical"
    android:padding="10dp"
    tools:context=".ui.RegisteredActivity">

    <EditText
        android:id="@+id/et_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/text_user_name"/>

    <EditText
        android:id="@+id/et_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/text_age"/>

    <EditText
        android:id="@+id/et_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/text_desc"
        android:lines="2"/>

    <RadioGroup
        android:id="@+id/mRadioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_boy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/text_boy"/>

        <RadioButton
            android:id="@+id/rb_girl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="@string/text_girl_f"/>

    </RadioGroup>


    <EditText
        android:id="@+id/et_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/text_password"
        android:inputType="textPassword"/>

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/text_password_again"
        android:inputType="textPassword"
        />

    <EditText
        android:id="@+id/et_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/text_email"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text_register_remind"
        android:textColor="@color/colorAccent"
        android:textSize="15sp"/>


    <Button
        android:id="@+id/btnRegistered"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_bg"
        android:text="@string/text_registered"
        android:textColor="@android:color/white"/>

</LinearLayout>

2.编写kotlin交互代码,代码如下:

package com.zrc.smartbutler.ui

import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.SaveListener
import com.zrc.smartbutler.R
import com.zrc.smartbutler.entity.MyUser
import com.zrc.smartbutler.utils.L
import kotlinx.android.synthetic.main.activity_registered.*


/**
 *
 *  项目名:  SmartButler
 *  包名:    com.zrc.smartbutler.ui
 *  文件名:   RegisteredActivity
 *  创建者:   张如成
 *  创建时间:  2020/5/7 22:11
 *  描述:    注册
 */
class RegisteredActivity : BaseActivty() {
    
    

    //性别
    private var isGender = true
    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_registered)
        //返回键
        back()

        initView();
    }

    private fun initView() {
    
    
        //注册按钮点击事件
        btnRegistered.setOnClickListener {
    
    
            //获取到输入框的值
            val name = et_user.text.trim()
            val age = et_age.text.trim()
            var desc = et_desc.text.trim()
            val pass = et_pass.text.trim()
            val password = et_password.text.trim()
            val email = et_email.text.trim()

            //判断是否为空
            if (!TextUtils.isEmpty(name) and !TextUtils.isEmpty(age) and
                    !TextUtils.isEmpty(pass) and
                    !TextUtils.isEmpty(password) and
                    !TextUtils.isEmpty(email)) {
    
    
                //判断两次密码是否一致
                if(pass.equals(password)){
    
    
                    //性别判断
                    mRadioGroup.setOnCheckedChangeListener {
    
     group, checkedId ->
                        if (checkedId == R.id.rb_boy) {
    
    
                            isGender = true
                        } else if (checkedId == R.id.rb_girl) {
    
    
                            isGender = false
                        }
                    }
                    //判断简介是否为空
                    if(TextUtils.isEmpty(desc)){
    
    
                        desc = "这个人很懒,什么都没有留下"
                    }

                    L().i("desc.toString() = "+ desc.toString())
                    L().i("age.toString() = "+ age.toString())
                    //注册
                    val user = MyUser()
                    user.setAge(age.toString().toInt())
                    user.setDesc(desc.toString())
                    user.setSex(isGender)
                    user.username = name.toString()
                    user.setPassword(password.toString())
                    user.email = email.toString()

                    user.signUp(object : SaveListener<MyUser>() {
    
    
                        override fun done(user: MyUser, e: BmobException) {
    
    
                            if (e == null) {
    
    
                                Toast.makeText(RegisteredActivity(),"注册成功",Toast.LENGTH_SHORT).show()
                                finish()
                            } else {
    
    
                                Toast.makeText(RegisteredActivity(),"注册失败",Toast.LENGTH_SHORT).show()
                            }
                        }
                    })
                }else{
    
    
                    Toast.makeText(this,"两次输入的密码不一致",Toast.LENGTH_SHORT).show()
                }

            }else{
    
    
                Toast.makeText(this,"输入框不能为空",Toast.LENGTH_SHORT).show()
            }
        }
    }
}

关于这段代码,做简要说明:
第一步:在entity包下,创建MyUser()实体类,继承BmobUser(),实体对象:用户名,密码,确认密码,性别,年龄,简介,邮箱,代码如下:

package com.zrc.smartbutler.entity

import cn.bmob.v3.BmobUser

/**
 *项目名:  SmartButler
 *包名:    com.zrc.smartbutler.entity
 *文件名:  MyUser
 *创建者:  张如成
 *创建时间: 2020/5/8 11:27
 *描述:    用户属性
 */
class MyUser (): BmobUser(){
    
    
    private var age = 0
    private var sex = false
    private var desc: String? = null

    fun getAge(): Int {
    
    
        return age
    }

    fun setAge(age: Int) {
    
    
        this.age = age
    }

    fun isSex(): Boolean {
    
    
        return sex
    }

    fun setSex(sex: Boolean) {
    
    
        this.sex = sex
    }

    fun getDesc(): String? {
    
    
        return desc
    }

    fun setDesc(desc: String?) {
    
    
        this.desc = desc
    }


}

第二步:获取交互界面用户填写的值,进行判断,如果为空,Toast提示输入框不能为空,如果不为空,调用registered,将数据提交到Bmob后台。当然,简介可以为空,当简介为空时,简介默认为:这个人很懒,什么都没留下。设置邮箱验证,邮箱验证之后,才可登录!

后台数据截图:
在这里插入图片描述

邮箱信息截图:
在这里插入图片描述

验证截图:
在这里插入图片描述
至此,用户注册功能实现!!!

三.用户管理—用户登录功能开发

功能截图:
在这里插入图片描述
1.在ui包下创建LoginActivity。
2。编写界面交互代码:

<?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"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".ui.LoginActivity">

    <ImageView

        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/et_name"
        android:hint="用户名"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="密码" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="记住密码"
        android:textColor="@color/colorRed" />



    <Button
        android:id="@+id/btnLogin"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/white"
        android:background="@drawable/button_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"/>
    <Button
        android:id="@+id/btn_registere"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/white"
        android:background="@drawable/button_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="记住密码?"
        android:textColor="@color/colorRed" />
</LinearLayout>

3.编写Kotlin逻辑交互代码,代码如下:

package com.zrc.smartbutler.ui

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import cn.bmob.v3.BmobUser
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.SaveListener
import com.google.android.material.snackbar.Snackbar
import com.zrc.smartbutler.MainActivity
import com.zrc.smartbutler.R
import com.zrc.smartbutler.entity.MyUser
import com.zrc.smartbutler.utils.L
import com.zrc.smartbutler.utils.ShareUtils
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_login.et_password
import kotlinx.android.synthetic.main.activity_registered.*


/**
 *  项目名:  SmartButler
 *  包名:    com.zrc.smartbutler.ui
 *  文件名:   LoginActivity
 *  创建者:   张如成
 *  创建时间:  2020/5/7 11:55
 *  描述:    登录
 */
class LoginActivity : AppCompatActivity() {
    
    

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

        initView();
    }

    private fun initView() {
    
    

        //忘记重置密码
        tv_forget.setOnClickListener{
    
    
            startActivity(Intent(this,ForgetPasswordActivity::class.java))
        }

        //设置选择状态
        val isCheck = ShareUtils().getBoolean(this,"keeppass",false)
        keep_password.isChecked = isCheck
        if(isCheck){
    
    
            //设置密码
            et_name.setText(ShareUtils().getString(this,"name",""))
            et_password.setText(ShareUtils().getString(this,"password",""))
        }
        //注册按钮
        btn_registere.setOnClickListener {
    
    
            startActivity(Intent(this,RegisteredActivity::class.java))
        }

        //登录按钮
        btnLogin.setOnClickListener {
    
    
            //1.获取输入框的值
            val name = et_name.text.toString().trim()
            val password = et_password.text.toString().trim()
            //2.判断是否为空
            if(!TextUtils.isEmpty(name) and !TextUtils.isEmpty(password)){
    
    
                //3.登录
                val user = MyUser()
                user.setUsername(name)
                user.setPassword(password)
                user.login(object : SaveListener<MyUser>() {
    
    
                    override fun done(bmobUser: MyUser?, e: BmobException) {
    
    
                        if (e == null) {
    
    
                            //val user: MyUser = BmobUser.getCurrentUser(MyUser::class.java)
                            startActivity(Intent(this@LoginActivity,MainActivity::class.java))
                            finish()
                        } else {
    
    
                            L().i(e.toString())
                            Toast.makeText(this@LoginActivity,"登录失败", Toast.LENGTH_SHORT).show()
                            //startActivity(Intent(this@LoginActivity,MainActivity::class.java))
                            //finish()
                        }


                    }
                })
                //L().i("用户名:"+name)
                //L().i("密码:"+password)


            }else{
    
    
                Toast.makeText(this,"输入框不能为空", Toast.LENGTH_SHORT).show()
            }
        }

    }
    //假设我现在输入用户名和密码,但是我不点击登录,就直接提出了
    override fun onDestroy() {
    
    
        super.onDestroy()

        //保存状态
        ShareUtils().putBoolean(this,"keeppass",keep_password.isChecked)

        //是否记住密码
        if(keep_password.isChecked){
    
    
            //记住用户名和密码
            ShareUtils().putString(this,"name",et_name.text.toString().trim())
            ShareUtils().putString(this,"password",et_password.text.toString().trim())

        }else{
    
    
            ShareUtils().deleShare(this,"name")
            ShareUtils().deleShare(this,"password")
        }
    }
}

此代码比较好理解,进入Activity后,判断上次是否保存账户密码,如果保存,显示在上面。点击登录按钮后,获取输入框用户填入的值,判断是否为空,如果不为空,进行传入后台,进行判断账户密码是否正确,如果正确,登录成功。退出程序时,进行判断是否要保存,并进行设置。

至此,用户登录功能实现!!!

四.用户管理—忘记重置密码开发

功能截图:
在这里插入图片描述
新建ForgetPasswordActivity,编写界面代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".ui.ForgetPasswordActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text_editor_password"
    android:textColor="@color/colorPrimary"
    android:textSize="30sp"/>

<EditText
    android:id="@+id/et_now"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/text_now_password"/>

<EditText
    android:id="@+id/et_new"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/text_new_password"/>

<EditText
    android:id="@+id/et_new_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/text_password_again"/>

<Button
    android:id="@+id/btn_update_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/button_bg"
    android:text="@string/text_editor_password"
    android:textColor="@android:color/white"/>

<TextView
    android:layout_marginTop="30dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text_forget_password"
    android:textColor="@color/colorPrimary"
    android:textSize="30sp"/>

<EditText
    android:id="@+id/et_email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/text_email"/>

<Button
    android:id="@+id/btn_forget_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/button_bg"
    android:text="@string/text_forget_password"
    android:textColor="@android:color/white"/>


</LinearLayout>

编写kotlin交互代码:

package com.zrc.smartbutler.ui

import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import cn.bmob.v3.BmobUser
import cn.bmob.v3.BmobUser.updateCurrentUserPassword
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.UpdateListener
import com.zrc.smartbutler.R
import com.zrc.smartbutler.entity.MyUser
import kotlinx.android.synthetic.main.activity_forget_password.*


/**
 *项目名:  SmartButler
 *包名:    com.zrc.smartbutler.ui
 *文件名:  GuideActivity
 *创建者:  张如成
 *创建时间: 2020/5/10 15:41
 *描述:    忘记/重置密码
 */
class ForgetPasswordActivity : BaseActivty() {
    
    

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

        intView()
    }

    //初始化View
    private fun intView() {
    
    
        //忘记密码点击事件
        btn_forget_password.setOnClickListener {
    
    
            //1.获取输入框的邮箱
            val email = et_email.text.toString().trim()
            //2.判断是否为空
            if(!TextUtils.isEmpty(email)){
    
    
                //3.发生邮件
                //TODO 此处替换为你的邮箱
                BmobUser.resetPasswordByEmail(email, object : UpdateListener() {
    
    
                    override fun done(e: BmobException) {
    
    
                        if (e == null) {
    
    
                            Toast.makeText(this@ForgetPasswordActivity,"邮箱已经发送至:"+email,Toast.LENGTH_SHORT).show()
                        } else {
    
    
                            Toast.makeText(this@ForgetPasswordActivity,"邮箱发送失败",Toast.LENGTH_SHORT).show()
                            Log.i("11111111",e.toString())
                        }
                    }
                })
            }else{
    
    
                Toast.makeText(this,"输入框不能为空",Toast.LENGTH_SHORT).show()
            }
        }




        //修改密码点击事件
        btn_update_password.setOnClickListener {
    
    
            //1.获取输入框的值
            val now = et_now.text.toString().trim()
            val news = et_new.text.toString().trim()
            val new_password = et_new_password.text.toString().trim()
            //2.判断是否为空
            if(!TextUtils.isEmpty(now) and !TextUtils.isEmpty(news) and !TextUtils.isEmpty(new_password)){
    
    
                //3.判断两次新密码是否一致
                if(news.equals(new_password)){
    
    
                    //4.重置密码

                    //4.重置密码
                    BmobUser.updateCurrentUserPassword(now, news, object : UpdateListener() {
    
    
                        override fun done(e: BmobException) {
    
    
                            if (e == null) {
    
    
                                Toast.makeText(this@ForgetPasswordActivity,
                                    R.string.reset_successfully, Toast.LENGTH_SHORT
                                ).show()
                                finish()
                            } else {
    
    
                                Toast.makeText(this@ForgetPasswordActivity,
                                    R.string.reset_failed,
                                    Toast.LENGTH_SHORT
                                ).show()
                                Log.i("111111111",e.toString())
                            }
                        }
                    })
                }else{
    
    
                    Toast.makeText(this,"两次输入密码不一致3",Toast.LENGTH_SHORT).show()
                }
            }else{
    
    
                Toast.makeText(this,"输入框不能为空",Toast.LENGTH_SHORT).show()
            }
        }
    }
}

忘记密码设有两类,有原始密码和无原始密码。当有原始密码时,输入旧密码,新密码,确认一遍进行修改。当无原始密码时,使用邮箱进行找回。下面附相应截图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
至此,忘记重置密码功能实现!!!

五.用户管理—个人中心数据处理开发

功能截图:
在这里插入图片描述
编写界面交互代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/user_frgament_bg"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/black"/>

            <TextView
                android:id="@+id/edit_user"
                android:layout_width="100dp"
                android:layout_height="30dp"
                android:layout_marginTop="20dp"
                android:background="@drawable/button_bg"
                android:gravity="center"
                android:text="编辑资料"
                android:textColor="@android:color/white"
                android:textSize="16sp"/>

        </LinearLayout>

        <!--用户名-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="姓名:"
                android:textSize="20dp"/>

            <EditText
                android:id="@+id/et_username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/colorPrimary"/>

        </LinearLayout>

        <!--性别-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="性别:"
                android:textSize="20dp"/>

            <EditText
                android:id="@+id/et_sex"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/colorPrimary"/>

        </LinearLayout>

        <!--年龄-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="年龄:"
                android:textSize="20dp"/>

            <EditText
                android:id="@+id/et_age"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/colorPrimary"/>

        </LinearLayout>


        <!--简介-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="简介:"
                android:textSize="20dp"/>

            <EditText
                android:id="@+id/et_desc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/colorPrimary"/>

        </LinearLayout>

        <Button
            android:id="@+id/btn_update_ok"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/button_bg"
            android:text="确定修改"
            android:textColor="@android:color/white"
            android:visibility="gone"/>

        <TextView
            android:id="@+id/tv_courier"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:text="物流查询"
            android:textSize="20dp"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:alpha="0.5"
            android:background="@color/colorPrimary"/>

        <TextView
            android:id="@+id/tv_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:text="归属地查询"
            android:textSize="20dp"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:alpha="0.5"
            android:background="@color/colorPrimary"/>

        <Button
            android:id="@+id/btn_exit_user"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/button_bg"
            android:text="退出登录"
            android:textColor="@android:color/white"/>

    </LinearLayout>
</ScrollView>

编写Kotlin交互代码:

package com.zrc.smartbutler.fragment

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import cn.bmob.v3.BmobUser
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.UpdateListener
import com.zrc.smartbutler.R
import com.zrc.smartbutler.entity.MyUser
import com.zrc.smartbutler.ui.LoginActivity
import kotlinx.android.synthetic.main.fragment_user.*
import kotlinx.android.synthetic.main.fragment_user.view.*

/**
 *项目名:  SmartButler
 *包名:    com.zrc.smartbutler.fragment
 *文件名:  UserFragment
 *创建者:  张如成
 *创建时间: 2020/5/6 9:14
 *描述:    个人中心
 */
class UserFragment:Fragment(){
    
    
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    
    
        val view: View = inflater.inflate(R.layout.fragment_user,null)

        //默认不可输入/不可输入
        setEnabled(false,view)

        //设置具体的值
        val userInfo = BmobUser.getCurrentUser(MyUser::class.java)
        et_username.setText(userInfo.username)
        et_age.setText(userInfo.getAge().toString() + "")
        et_sex.setText(if (userInfo.isSex()) getString(R.string.text_boy) else getString(R.string.text_girl_f))
        et_desc.setText(userInfo.getDesc())
        return view
    }

    private fun initView(view:View) {
    
    
        //退出登录
        view.btn_exit_user.setOnClickListener {
    
    
            //清楚缓存对象
            BmobUser.logOut()
            //现在的currentUser是null了
            // 现在的currentUser是null了
            val currentUser: BmobUser = BmobUser.getCurrentUser(MyUser::class.java)
            startActivity(Intent(activity, LoginActivity::class.java))
            activity!!.finish()
        }

        //编辑资料
        view.edit_user.setOnClickListener {
    
    
            setEnabled(true,view)
            btn_update_ok.visibility = View.VISIBLE
        }

        view.btn_update_ok.setOnClickListener{
    
    
            //1.拿到输入框的值
            val username = et_username.text.toString()
            val age = et_age.text.toString()
            val sex = et_sex.text.toString()
            val desc = et_desc.text.toString()


            //2.判断是否为空
            if (!TextUtils.isEmpty(username) and !TextUtils.isEmpty(age) and !TextUtils.isEmpty(sex)) {
    
    
                //3.更新属性
                val user = MyUser()
                user.username = username
                user.setAge(age.toInt())
                //性别
                if (sex == getString(R.string.text_boy)) {
    
    
                    user.setSex(true)
                } else {
    
    
                    user.setSex(false)
                }
                //简介
                if (!TextUtils.isEmpty(desc)) {
    
    
                    user.setDesc(desc)
                } else {
    
    
                    user.setDesc(getString(R.string.text_nothing))
                }
                val bmobUser = BmobUser.getCurrentUser<BmobUser>(BmobUser::class.java)
                user.update(bmobUser.objectId, object : UpdateListener() {
    
    
                    override fun done(e: BmobException) {
    
    
                        if (e == null) {
    
    
                            //修改成功
                            setEnabled(false,view)
                            btn_update_ok.visibility = View.GONE
                            Toast.makeText(
                                activity,
                                R.string.text_editor_success,
                                Toast.LENGTH_SHORT
                            ).show()
                        } else {
    
    
                            Toast.makeText(
                                activity,
                                R.string.text_editor_failure,
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    }
                })
            } else {
    
    
                Toast.makeText(
                    activity,
                    getString(R.string.text_tost_empty),
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

    }

    //控制焦点
    private fun setEnabled(`is`: Boolean,view: View) {
    
    
        view.et_username.isEnabled = `is`
        view.et_sex.isEnabled = `is`
        view.et_age.isEnabled = `is`
        view.et_desc.isEnabled = `is`
    }
}

这段代码,就是对退出登录,编辑资料信息进行操作,具体看代码即可!!!

至此,个人中心数据处理实现实现!!!

用户注册/登录/忘记重置密码/个人数据编辑开发完成,下篇文章将针对快递及号码地查询进行开发,欢迎关注后续更新!!!

猜你喜欢

转载自blog.csdn.net/weixin_43912367/article/details/106002810
今日推荐