Skip to content

Commit 8b19dd1

Browse files
committed
add builder to verify input
1 parent 66e9e58 commit 8b19dd1

File tree

8 files changed

+106
-25
lines changed

8 files changed

+106
-25
lines changed

.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

VerifyCodeEditText/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
defaultConfig {
1313
minSdkVersion 17
1414
targetSdkVersion 30
15-
versionCode 1
16-
versionName "1.0"
15+
versionCode 2
16+
versionName "1.1"
1717

1818
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1919
consumerProguardFiles "consumer-rules.pro"

VerifyCodeEditText/src/main/java/com/jakode/verifycodeedittext/VerifyCodeEditText.kt

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.jakode.verifycodeedittext
33
import android.annotation.SuppressLint
44
import android.content.Context
55
import android.content.res.TypedArray
6+
import android.graphics.Color
67
import android.graphics.drawable.Drawable
78
import android.text.InputType
89
import android.util.AttributeSet
@@ -18,11 +19,7 @@ import android.widget.TextView
1819
import androidx.core.content.ContextCompat
1920
import androidx.core.content.res.ResourcesCompat
2021

21-
class VerifyCodeEditText(
22-
context: Context,
23-
attrs: AttributeSet?,
24-
defStyleAttr: Int
25-
) : LinearLayout(context, attrs, defStyleAttr) {
22+
class VerifyCodeEditText(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : LinearLayout(context, attrs, defStyleAttr) {
2623
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
2724
constructor(context: Context) : this(context, null)
2825

@@ -57,18 +54,22 @@ class VerifyCodeEditText(
5754
private var bottomIconWidth = 0
5855

5956
init {
60-
setupField(attrs, defStyleAttr)
6157
// Layout Settings
6258
orientation = HORIZONTAL
63-
layoutDirection = LAYOUT_DIRECTION_LTR
6459
gravity = Gravity.CENTER
6560
isFocusableInTouchMode = true
6661

6762
// Initialize
6863
if (!::stringBuilder.isInitialized) stringBuilder = StringBuilder()
6964
if (!::viewList.isInitialized) viewList = ArrayList()
7065

71-
// Setup Bottom Icons
66+
setupField(attrs, defStyleAttr) // Setup filed
67+
setupBottomIcon() // Setup Bottom Icons
68+
}
69+
70+
private fun setupBottomIcon() {
71+
viewList.forEach { removeView(it) }
72+
viewList.clear()
7273
for (i in 0 until viewCount) {
7374
val underLineCodeView: TextView = getUnderLineIcon(i)
7475
viewList.add(underLineCodeView)
@@ -210,4 +211,66 @@ class VerifyCodeEditText(
210211

211212
private fun getColorFromRes(colorRes: Int) = ContextCompat.getColor(context, colorRes)
212213
private fun getDrawableFromRes(drawableRes: Int) = ContextCompat.getDrawable(context, drawableRes)
214+
215+
class Builder(initialize: Builder.() -> Unit) {
216+
init { initialize() }
217+
218+
private var textHolder: Text? = null
219+
private var bottomIconHolder: BottomIcon? = null
220+
private var verifyCell: VerifyCell? = null
221+
222+
fun text(initialize: Text.() -> Unit) {
223+
textHolder = Text().apply { initialize() }
224+
}
225+
226+
fun bottomIcon(initialize: BottomIcon.() -> Unit) {
227+
bottomIconHolder = BottomIcon().apply { initialize() }
228+
}
229+
230+
fun verifyCell(initialize: VerifyCell.() -> Unit) {
231+
verifyCell = VerifyCell().apply { initialize() }
232+
}
233+
234+
fun build(context: Context): VerifyCodeEditText {
235+
return VerifyCodeEditText(context).apply {
236+
textHolder?.apply {
237+
textSize = size
238+
textColor = color
239+
textFontRes = fontRes
240+
}
241+
bottomIconHolder?.apply {
242+
bottomSelectedIcon = selectedIcon
243+
bottomUnSelectedIcon = unSelectedIcon
244+
bottomErrorIcon = errorIcon
245+
bottomIconHeight = iconHeight
246+
bottomIconWidth = iconWidth
247+
}
248+
verifyCell?.apply {
249+
viewCount = count.value
250+
itemCenterSpaceSize = spaceSize
251+
}
252+
}.also { it.setupBottomIcon() }
253+
}
254+
255+
data class Text(
256+
var size: Float = 18f,
257+
var color: Int = Color.parseColor("#000000"),
258+
var fontRes: Int = Int.MIN_VALUE
259+
)
260+
261+
data class BottomIcon(
262+
var selectedIcon: Drawable? = null,
263+
var unSelectedIcon: Drawable? = null,
264+
var errorIcon: Drawable? = null,
265+
var iconHeight: Int = 1,
266+
var iconWidth: Int = 32,
267+
)
268+
269+
data class VerifyCell(
270+
var count: ViewCount = ViewCount.Four,
271+
var spaceSize: Int = 18
272+
)
273+
274+
enum class ViewCount(val value: Int) { Four(4), Five(5), Six(6) }
275+
}
213276
}

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ android {
1111
applicationId "com.jakode.verifycodeedittexttest"
1212
minSdkVersion 17
1313
targetSdkVersion 30
14-
versionCode 1
15-
versionName "1.0"
14+
versionCode 2
15+
versionName "1.1"
1616

1717
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1818
}

app/src/main/java/com/jakode/verifycodeedittexttest/MainActivity.kt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,33 @@ class MainActivity : AppCompatActivity() {
1010
super.onCreate(savedInstanceState)
1111
setContentView(R.layout.activity_main)
1212

13-
val verifyCodeEditText = findViewById<VerifyCodeEditText>(R.id.verifyCodeEditText)
13+
val verifyCodeEditText = findViewById<VerifyCodeEditText>(R.id.verify)
1414
verifyCodeEditText.setCompleteListener { complete ->
1515
if (complete) {
16-
Toast.makeText(this@MainActivity, verifyCodeEditText.text, Toast.LENGTH_SHORT).show()
16+
Toast.makeText(this@MainActivity, verifyCodeEditText.text, Toast.LENGTH_SHORT)
17+
.show()
1718
verifyCodeEditText.setCodeItemErrorLineDrawable()
1819
}
1920
}
2021
verifyCodeEditText.text = "99999"
22+
23+
/*val verifyCodeEditText = VerifyCodeEditText.Builder {
24+
text {
25+
size = 20F
26+
color = Color.parseColor("#000000")
27+
}
28+
bottomIcon {
29+
iconHeight = 5
30+
iconWidth = 60
31+
selectedIcon = ContextCompat.getDrawable(this@MainActivity, R.drawable.bottom_selected_icon)
32+
unSelectedIcon = ContextCompat.getDrawable(this@MainActivity, R.drawable.bottom_unselected_icon)
33+
errorIcon = ContextCompat.getDrawable(this@MainActivity, R.drawable.bottom_error_icon)
34+
}
35+
verifyCell {
36+
count = VerifyCodeEditText.Builder.ViewCount.Five
37+
spaceSize = 48
38+
}
39+
}.build(context = this)
40+
findViewById<ConstraintLayout>(R.id.layout).addView(verifyCodeEditText)*/
2141
}
2242
}

app/src/main/res/drawable/stroke.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.

app/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/layout"
56
android:layout_width="match_parent"
67
android:layout_height="match_parent"
78
tools:context=".MainActivity">
89

910
<com.jakode.verifycodeedittext.VerifyCodeEditText
10-
android:id="@+id/verifyCodeEditText"
11+
android:id="@+id/verify"
1112
android:layout_width="wrap_content"
1213
android:layout_height="wrap_content"
13-
android:background="@drawable/stroke"
14+
android:layoutDirection="ltr"
1415
android:paddingBottom="12dp"
1516
app:BottomIconWidth="40dp"
1617
app:ItemSpaceSize="28dp"
1718
app:TextColor="@color/black"
1819
app:TextSize="16sp"
19-
app:ViewCount="Five"
2020
app:layout_constraintBottom_toBottomOf="parent"
2121
app:layout_constraintEnd_toEndOf="parent"
2222
app:layout_constraintStart_toStartOf="parent"

0 commit comments

Comments
 (0)