写点什么

Koltin28

用户头像
Android架构
关注
发布于: 5 小时前

// 请求验证码,其中 country 表示国家代码,如“86”;phone 表示手机号码,如“13800138000”


SMSSDK.getVerificationCode("86", phone)


//开启倒计时


tv_user_code.isEnabled = false


Thread(CutDownTask()).start()


}


}


iv_login.setOnClickListener {


//提交验证码


val phone = et_user_phone.text.toString().trim()


val code = et_user_code.text.toString().trim()


//下面是短信验证码的校验,先屏蔽


// if (SMSUtil.judgePhoneNums(this, phone) && !TextUtils.isEmpty(code)) {


// // 提交验证码,其中的 code 表示验证码,如“1357”


// SMSSDK.submitVerificationCode("86", phone, code)


// }


//直接登录登录外卖服务器,省去短信验证码验证阶段


loginActivityPresenter.loginByPhone(phone)


}


}


companion object {


val TIME_MINUS = -1 //剩余时间的消息


val TIME_IS_OUT = 0 //60s 超时


}


//创建一个 handler 来刷新 UI


val handler = @SuppressLint("HandlerLeak")


object : Handler(){


override fun handleMessage(msg: Message?) {


super.handleMessage(msg)


when(msg!!.what){


TIME_MINUS -> tv_user_code.text = "剩余时间(${time})秒"


TIME_IS_OUT -> {


tv_user_code.isEnabled = true


tv_user_code.text = "点击重发"


time = 60


}


}


}


}


//倒计时的实现 60s


var time = 60


inner class CutDownTask: Runnable {


override fun run() {


//倒计时循环


while (time>0){


//刷新剩余时间,当前子


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


线程,使用 handler


handler.sendEmptyMessage(TIME_MINUS)


SystemClock.sleep(999)


time --


}


handler.sendEmptyMessage(TIME_IS_OUT)


}


}


// 使用完 EventHandler 需注销,否则可能出现内存泄漏


override fun onDestroy() {


super.onDestroy()


SMSSDK.unregisterEventHandler(eh)


}


fun onLoginSuccess() {


finish()


toast("登录成功")


}


fun onLoginFailed(){


toast("登录失败")


}


}


LoginActivityPresenter.kt 登录的网络请求逻辑


package com.example.takeout.presenter


import com.example.takeout.beans.User


import com.example.takeout.ui.activity.LoginActivity


import com.example.takeout.utils.TakeoutApp


import com.google.gson.Gson


//继承 NetPresenter


class LoginActivityPresenter(val loginActivity: LoginActivity) : NetPresenter(){


//使用手机号码登录的业务


fun loginByPhone(phone:String) {


val homeCall = takeoutService.loginByPhone()


homeCall.enqueue(callback)


}


override fun parserJson(json: String) {


val user = Gson().fromJson(json, User::class.java)


if (user != null) {


//缓存到内存中


TakeoutApp.sUser = user


loginActivity.onLoginSuccess()


} else{


loginActivity.onLoginFailed()


}


}


}


UserFragment.kt 登录成功以后用户展示界面的刷新


package com.example.takeout.ui.fragment


import android.app.Fragment


import android.content.Intent


import android.os.Bundle


import android.view.LayoutInflater


import android.view.View


import android.view.ViewGroup


import android.widget.ImageView


import android.widget.LinearLayout


import com.example.takeout.R


import com.example.takeout.ui.activity.LoginActivity


import com.example.takeout.utils.TakeoutApp


import kotlinx.android.synthetic.main.fragment_user.*


import org.jetbrains.anko.find


class UserFragment : Fragment() {


lateinit var ll_userinfo: LinearLayout


lateinit var ivLogin:ImageView


override fun onCreateView(


inflater: LayoutInflater,


container: ViewGroup?,


savedInstanceState: Bundle?


): View? {


val userview = View.inflate(activity, R.layout.fragment_user, null)


ll_userinfo = userview.find<LinearLayout>(R.id.ll_userinfo)


ivLogin = userview.find<ImageView>(R.id.login)


ivLogin.setOnClickListener {


val intent = Intent(activity, LoginActivity::class.java)


activity.startActivity(intent)


}


return userview


}


override fun onStart() {


super.onStart()


//展示登录成功后的 ui 效果


val user = TakeoutApp.sUser


if (user.id == -1) {


//未登录


ll_userinfo.visibility = View.GONE


ivLogin.visibility = View.VISIBLE


} else {


ivLogin.visibility = View.GONE


ll_userinfo.visibility = View.VISIBLE


username.text = "欢迎您,${user.name}"


phone.text = user.phone


}


}


}


TakeoutApp.kt 保存登录的数据


package com.example.takeout.utils


import android.app.Application


import com.example.takeout.beans.User


class TakeoutApp : Application() {


companion object {


var sUser: User = User()


lateinit var sInstance: TakeoutApp


}


//应用程序的入口


override fun onCreate() {


super.onCreate()


sInstance = this


sUser.id = -1 //未登录用户 id=-1


}


}


User.kt 用户的 bean 数据,对 bean 的数据进行初始化


package com.example.takeout.beans


import java.io.Serializable


data class User(


var id: Int = 0,


var name: String? = null,


var balance: Float = 0.toFloat(),


var discount: Int = 0,


var integral: Int = 0,


var phone: String? = null


) : Serializable {


// var id: Long = 0

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
Koltin28