Skip to content

Commit

Permalink
change key listener to make compatible with older android versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Masoud Ashrafzadeh committed Feb 26, 2020
1 parent a33ba72 commit f5dfbf0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/sma6871/cardentryapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MainActivity : AppCompatActivity() {
card.setTextColor(ContextCompat.getColor(this,R.color.green))
}
card.onPinChange { isComplete, length ->
Toast.makeText(this,"is Completed=$isComplete, length=$length",Toast.LENGTH_SHORT).show()
// Toast.makeText(this,"is Completed=$isComplete, length=$length",Toast.LENGTH_SHORT).show()
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
android:layout_marginEnd="16dp"
android:maxLength="16"
android:selectAllOnFocus="false"
android:text="6104337982412311"
tools:text="6104337982412311"
android:textSize="16sp"
app:ce_number_count="16"
app:ce_part_count="4"
Expand Down
46 changes: 34 additions & 12 deletions cardentry/src/main/java/com/sma6871/cardentry/CardEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import android.util.AttributeSet
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import android.view.inputmethod.InputConnectionWrapper
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.content.ContextCompat
import java.util.*
Expand Down Expand Up @@ -76,8 +79,8 @@ class CardEntry : AppCompatEditText {
}
var oldText = ""

private val partNumbersCount:Int by lazy {
maxLength/partCount
private val partNumbersCount: Int by lazy {
maxLength / partCount
}

private fun String.getChunked(): String {
Expand All @@ -100,6 +103,7 @@ class CardEntry : AppCompatEditText {

})
}

/**
* Call this method to get raw text (without spaces)
* */
Expand Down Expand Up @@ -234,21 +238,39 @@ class CardEntry : AppCompatEditText {

mLineSpacingAnimated = if (hasAnimation) 0f else mLineSpacing


}

override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_DEL) {
if (selectionStart == selectionEnd && selectionStart > maxLength / partCount) {
if (text?.get(selectionStart - 1) == ' ') {
val startIndex = selectionStart - spaceCount
setText(text?.removeRange(startIndex, selectionStart).toString().getChunked())
if (startIndex >= 0)
setSelection(startIndex)
return true
override fun onCreateInputConnection(outAttrs: EditorInfo?): InputConnection {
return CardEntryInputConnection(super.onCreateInputConnection(outAttrs), true, this)
}

private class CardEntryInputConnection(target: InputConnection, mutable: Boolean, val editText: CardEntry) : InputConnectionWrapper(target, mutable) {

override fun sendKeyEvent(event: KeyEvent?): Boolean {
if (event?.keyCode == KeyEvent.KEYCODE_DEL && event.action == KeyEvent.ACTION_UP) {
if (editText.selectionStart == editText.selectionEnd && editText.selectionStart > editText.maxLength / editText.partCount) {
if (editText.text?.get(editText.selectionStart - 1) == ' ') {
val startIndex = editText.selectionStart - editText.spaceCount
editText.setText(editText.text?.removeRange(startIndex, editText.selectionStart).toString().replace(" ", "").chunked(4).joinToString(separator = editText.spaces))
if (startIndex >= 0)
editText.setSelection(startIndex)
return true
}
}
}
return super.sendKeyEvent(event)
}
return super.onKeyUp(keyCode, event)

override fun deleteSurroundingText(beforeLength: Int, afterLength: Int): Boolean {
if (beforeLength == 1 && afterLength == 0) {
// backspace
return (sendKeyEvent( KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
and sendKeyEvent( KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)))
}
return super.deleteSurroundingText(beforeLength, afterLength)
}

}

private fun getCharSize(s: String): Float {
Expand Down

0 comments on commit f5dfbf0

Please sign in to comment.