Skip to content

Commit

Permalink
pin change listener added
Browse files Browse the repository at this point in the history
  • Loading branch information
sma6871 committed Apr 7, 2019
1 parent 5bd9401 commit 54afc56
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ package com.hm60.pinentryview_example

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

pin.onPinChange { x, y ->
Toast.makeText(this, "$x $y", Toast.LENGTH_SHORT).show()

}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
tools:context=".MainActivity">

<com.hm60.pinentryview.PinEntryView
android:id="@+id/pin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
26 changes: 23 additions & 3 deletions pinentryview/src/main/java/com/hm60/pinentryview/PinEntryView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import androidx.core.content.ContextCompat

class PinEntryView : AppCompatEditText {

private var maxLength = 4 // default length
var maxLength = 4 // default length
private var mSpace = toPxF(16)
private var mCharSize = toPxF(32)
private var mLineSpacing = toPxF(12)
Expand All @@ -30,13 +30,13 @@ class PinEntryView : AppCompatEditText {
private var animatedAlpha = 255


private var linePaint: Paint = Paint().apply {
var linePaint: Paint = Paint().apply {
isAntiAlias = true
color = getColor(R.color.silverGray)
style = Paint.Style.FILL
}

private var textPaint: TextPaint = TextPaint().apply {
var textPaint: TextPaint = TextPaint().apply {
isAntiAlias = true
color = getColor(R.color.coal)
textSize = spToPxF(18)
Expand All @@ -46,6 +46,25 @@ class PinEntryView : AppCompatEditText {
var lineColor = getColor(R.color.silverGray)
var filledLineColor = getColor(R.color.green)


fun onPinChange(onChange: (isComplete: Boolean, length: Int) -> Unit) {
addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {
onChange(s.length == maxLength, s.length)

}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

}

})
}

constructor(context: Context) : super(context) {
init(context, null)
}
Expand Down Expand Up @@ -270,4 +289,5 @@ class PinEntryView : AppCompatEditText {
valueAnimator.start()
}


}

0 comments on commit 54afc56

Please sign in to comment.