@@ -85,12 +85,13 @@ class OtpCodeView @JvmOverloads constructor(
8585 val allViews = getAllViews()
8686 val listTextView = allViews.drop(1 ).map { it as TextView }
8787 val mainEditText = allViews[0 ] as EditText
88- mainEditText.filters = arrayOf(InputFilter .LengthFilter (listTextView.size ))
88+ mainEditText.filters = arrayOf(InputFilter .LengthFilter (codeLength ))
8989 mainEditText.setOnClickListener { mainEditText.setSelection(mainEditText.text.length) }
9090 mainEditText.doOnTextChanged { text, _, _, _ -> handleShowCode(text, listTextView) }
91- listTextView.forEach {
92- it.setTextColor(codeColor)
93- it.background = getDrawable(codeStrokeColor, false )
91+ listTextView.forEachIndexed { index, textview ->
92+ if (index >= codeLength) textview.visibility = View .GONE
93+ textview.setTextColor(codeColor)
94+ textview.background = getDrawable(codeStrokeColor, false )
9495 }
9596 }
9697
@@ -99,7 +100,7 @@ class OtpCodeView @JvmOverloads constructor(
99100 text.toString().apply {
100101 handleBackgroundHasCode(listTextView)
101102 handleBackgroundNotHasCode(listTextView)
102- if (length == listTextView.size ) {
103+ if (length == codeLength ) {
103104 handleBackgroundDoneEnter(listTextView)
104105 onVerifyDone?.invoke(this )
105106 binding.root.hideKeyBoard()
@@ -108,15 +109,15 @@ class OtpCodeView @JvmOverloads constructor(
108109 }
109110
110111 private fun String.handleBackgroundDoneEnter (listTextView : List <TextView >) {
111- (listTextView.indices ).forEach {
112+ (0 until codeLength ).forEach {
112113 listTextView[it].background = getDrawable(codeDoneStrokeColor, true )
113114 listTextView[it].setTextColor(codeColor)
114115 listTextView[it].text = get(it).toString()
115116 }
116117 }
117118
118119 private fun String.handleBackgroundNotHasCode (listTextView : List <TextView >) {
119- (length until listTextView.size ).forEach {
120+ (length until codeLength ).forEach {
120121 listTextView[it].background = getDrawable(codeStrokeColor, false )
121122 listTextView[it].setTextColor(codeColor)
122123 listTextView[it].text = " "
@@ -164,7 +165,7 @@ class OtpCodeView @JvmOverloads constructor(
164165 binding.tvCodeInvalid.text = error
165166 val views = getAllViews()
166167 val tvs = views.drop(1 ).map { it as TextView }
167- (tvs.indices ).forEach {
168+ (0 until codeLength ).forEach {
168169 tvs[it].background = getDrawable(codeStrokeErrorColor, true )
169170 tvs[it].setTextColor(codeErrorColor)
170171 }
0 commit comments