package com.sinata.xqmuse.ui.login
|
|
import android.content.Context
|
import android.os.CountDownTimer
|
import android.text.Editable
|
import android.text.TextWatcher
|
import android.view.KeyEvent
|
import android.view.MotionEvent
|
import android.view.inputmethod.InputMethodManager
|
import androidx.core.os.bundleOf
|
import cn.sinata.xldutils.utils.*
|
import cn.sinata.xldutils.utils.hideBankCard
|
import com.sinata.xqmuse.R
|
import com.sinata.xqmuse.dialog.MsgDialog
|
import com.sinata.xqmuse.network.HttpManager
|
import com.sinata.xqmuse.network.request
|
import com.sinata.xqmuse.ui.TransparentStatusBarActivity
|
import com.sinata.xqmuse.utils.Const
|
import kotlinx.android.synthetic.main.activity_input_code.*
|
import org.jetbrains.anko.startActivity
|
import java.util.*
|
|
class InputCodeActivity:TransparentStatusBarActivity() {
|
override fun setContentView() = R.layout.activity_input_code
|
|
private val phone by lazy { intent.getStringExtra("phone")?:"" }
|
private val codeEts by lazy { arrayListOf(et_1,et_2,et_3,et_4,et_5,et_6) }
|
private var cursorIndex = 0 //当前焦点位置
|
private var code = StringBuffer()
|
|
private val timer by lazy {
|
object : CountDownTimer(60000, 1000) {
|
override fun onFinish() {
|
tv_3.text = ""
|
tv_get_code.isEnabled = true
|
tv_get_code.alpha = 1f
|
}
|
|
override fun onTick(millisUntilFinished: Long) {
|
val format = "%ds后可重新发送验证码".format(millisUntilFinished / 1000)
|
tv_3.text = SpanBuilder(format).color(this@InputCodeActivity,0,format.indexOf("后"),R.color.colorPrimary).build()
|
tv_get_code.isEnabled = false
|
tv_get_code.alpha = 0.5f
|
}
|
}
|
}
|
|
override fun initClick() {
|
tv_get_code.setOnClickListener {
|
tv_get_code.isEnabled = false
|
tv_get_code.alpha = 0.5f
|
HttpManager.getCode(phone, 3).request(this@InputCodeActivity, success = { _, _ ->
|
timer.start()
|
}) { _, _ ->
|
tv_get_code.isEnabled = true
|
tv_get_code.alpha = 1f}
|
}
|
|
tv_action.setOnClickListener {
|
val toString = code.toString()
|
if (toString.length == 6){
|
HttpManager.verifyPhone(phone,toString).request(this,success = {_,data->
|
if (data?.successFlag == true){
|
val msgDialog = MsgDialog()
|
msgDialog.arguments = bundleOf("msg" to "手机号验证成功","title" to "验证成功")
|
msgDialog.setDismissCallback(object :MsgDialog.OnDismiss{
|
override fun onDismiss() {
|
startActivity<SetPwdActivity>("secret" to data.secret,"phone" to phone)
|
finish()
|
}
|
})
|
msgDialog.showAllowingStateLoss(supportFragmentManager,"check")
|
}else{
|
et_6.setText("")
|
tv_action.isEnabled = true
|
val msgDialog = MsgDialog()
|
msgDialog.arguments = bundleOf("msg" to "手机号验证失败","title" to "验证失败")
|
msgDialog.showAllowingStateLoss(supportFragmentManager,"check")
|
}
|
}){_,_->
|
tv_action.isEnabled = true
|
}
|
}else
|
myToast("请输入6位验证码")
|
}
|
|
codeEts.forEach {
|
it.addTextChangedListener(object : TextWatcher {
|
override fun afterTextChanged(s: Editable?) {
|
if (s?.isNotEmpty() == true) {
|
code.append(s)
|
if (cursorIndex == 5) { //输满6位数,提交
|
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
imm.hideSoftInputFromWindow(codeEts[cursorIndex].windowToken, 0) //强制隐藏键盘
|
} else { //不满6位数,进入下一位
|
cursorIndex++
|
codeEts[cursorIndex].requestFocus()
|
}
|
} else {
|
// code.deleteCharAt(code.lastIndex)
|
// if (cursorIndex != 0) { //还没删到第一个
|
// cursorIndex--
|
// codeEts[cursorIndex].requestFocus() //焦点交给上一个
|
// }
|
if (code.isNotEmpty()){
|
cursorIndex = 0
|
code.delete(0,code.length)
|
codeEts.forEach {
|
it.setText("")
|
}
|
et_1.requestFocus()
|
}
|
|
}
|
}
|
|
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
}
|
|
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
}
|
})
|
|
it.setOnKeyListener { v, keyCode, event ->
|
if (keyCode == KeyEvent.KEYCODE_DEL&&it.text.isEmpty()&&cursorIndex!=0){
|
code.delete(0,code.length)
|
cursorIndex = 0
|
codeEts.forEach {
|
it.setText("")
|
}
|
et_1.requestFocus()
|
}
|
return@setOnKeyListener false
|
}
|
}
|
}
|
|
override fun initView() {
|
if (!SPUtils.instance().getString(Const.User.TOKEN).isNullOrEmpty())
|
title = "修改密码"
|
tv_2.text = "验证码已发送至:${phone.formatPhone()}"
|
tv_get_code.isEnabled = false
|
tv_get_code.alpha = 0.5f
|
timer.start()
|
et_1.postDelayed({
|
try {
|
et_1.requestFocus()
|
(getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
|
.showSoftInput(et_1, InputMethodManager.SHOW_FORCED)
|
}catch (e:Exception){
|
|
}
|
},500)
|
}
|
}
|