lmw
2025-04-24 718f31c92e2029d05260810435a2c70cef6e6ce5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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)
    }
}