From f5dfbf0a465db21c24b1dc905c361c174fb22c7c Mon Sep 17 00:00:00 2001 From: Masoud Ashrafzadeh Date: Wed, 26 Feb 2020 14:01:53 +0330 Subject: [PATCH] change key listener to make compatible with older android versions --- .../com/sma6871/cardentryapp/MainActivity.kt | 2 +- app/src/main/res/layout/activity_main.xml | 2 +- .../java/com/sma6871/cardentry/CardEntry.kt | 46 ++++++++++++++----- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/sma6871/cardentryapp/MainActivity.kt b/app/src/main/java/com/sma6871/cardentryapp/MainActivity.kt index a98da60..13b20b7 100644 --- a/app/src/main/java/com/sma6871/cardentryapp/MainActivity.kt +++ b/app/src/main/java/com/sma6871/cardentryapp/MainActivity.kt @@ -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() } } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c1d6e9b..de9ab45 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -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" diff --git a/cardentry/src/main/java/com/sma6871/cardentry/CardEntry.kt b/cardentry/src/main/java/com/sma6871/cardentry/CardEntry.kt index 538f3a8..dc4226f 100644 --- a/cardentry/src/main/java/com/sma6871/cardentry/CardEntry.kt +++ b/cardentry/src/main/java/com/sma6871/cardentry/CardEntry.kt @@ -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.* @@ -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 { @@ -100,6 +103,7 @@ class CardEntry : AppCompatEditText { }) } + /** * Call this method to get raw text (without spaces) * */ @@ -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 {