@@ -4,10 +4,16 @@ import android.os.Bundle
4
4
import android.widget.EditText
5
5
import androidx.annotation.StringRes
6
6
import androidx.appcompat.app.AppCompatActivity
7
+ import androidx.constraintlayout.widget.ConstraintLayout
7
8
import com.github.ihermandev.formatwatcher.FormatWatcher
9
+ import com.google.android.material.snackbar.Snackbar
8
10
9
11
class MainActivity : AppCompatActivity () {
10
12
13
+ private val rootContainer by lazy {
14
+ findViewById<ConstraintLayout >(R .id.container)
15
+ }
16
+
11
17
private val editText1 by lazy {
12
18
findViewById<EditText >(R .id.editText1)
13
19
}
@@ -36,25 +42,69 @@ class MainActivity : AppCompatActivity() {
36
42
findViewById<EditText >(R .id.editText7)
37
43
}
38
44
45
+ private val formatWatcher1 by lazy {
46
+ FormatWatcher (getStringResource(R .string.format_set1),
47
+ placeholderInFormat = ' #' )
48
+ }
49
+
50
+ private val formatWatcher2 by lazy {
51
+ FormatWatcher (getStringResource(R .string.format_set2),
52
+ placeholderInFormat = ' *' )
53
+ }
54
+
55
+ private val formatWatcher3 by lazy {
56
+ FormatWatcher (getStringResource(R .string.format_set3),
57
+ placeholderInFormat = ' #' )
58
+ }
59
+
60
+ private val formatWatcher4 by lazy {
61
+ FormatWatcher (getStringResource(R .string.format_set4),
62
+ placeholderInFormat = ' #' )
63
+ }
64
+
65
+ private val formatWatcher5 by lazy {
66
+ FormatWatcher (getStringResource(R .string.format_set5),
67
+ placeholderInFormat = ' *' )
68
+ }
69
+
70
+ private val formatWatcher6 by lazy {
71
+ FormatWatcher (getStringResource(R .string.format_set6),
72
+ placeholderInFormat = ' #' )
73
+ }
74
+
75
+ private val formatWatcher7 by lazy {
76
+ FormatWatcher (getStringResource(R .string.format_set7),
77
+ placeholderInFormat = ' #' )
78
+ }
79
+
39
80
override fun onCreate (savedInstanceState : Bundle ? ) {
40
81
super .onCreate(savedInstanceState)
41
82
setContentView(R .layout.activity_main)
42
83
43
- editText1.addTextChangedListener(FormatWatcher (getStringResource(R .string.format_set1),
44
- placeholderInFormat = ' #' ))
45
- editText2.addTextChangedListener(FormatWatcher (getStringResource(R .string.format_set2),
46
- placeholderInFormat = ' *' ))
47
- editText3.addTextChangedListener(FormatWatcher (getStringResource(R .string.format_set3),
48
- placeholderInFormat = ' #' ))
49
- editText4.addTextChangedListener(FormatWatcher (getStringResource(R .string.format_set4),
50
- placeholderInFormat = ' #' ))
51
- editText5.addTextChangedListener(FormatWatcher (getStringResource(R .string.format_set5),
52
- placeholderInFormat = ' *' ))
53
- editText6.addTextChangedListener(FormatWatcher (getStringResource(R .string.format_set6),
54
- placeholderInFormat = ' #' ))
55
- editText7.addTextChangedListener(FormatWatcher (getStringResource(R .string.format_set7),
56
- placeholderInFormat = ' #' ))
84
+ editText1.handleEditTextFormatting(formatWatcher1)
85
+ editText2.handleEditTextFormatting(formatWatcher2)
86
+ editText3.handleEditTextFormatting(formatWatcher3)
87
+ editText4.handleEditTextFormatting(formatWatcher4)
88
+ editText5.handleEditTextFormatting(formatWatcher5)
89
+ editText6.handleEditTextFormatting(formatWatcher6)
90
+ editText7.handleEditTextFormatting(formatWatcher7)
91
+ }
92
+
93
+ private fun EditText.handleEditTextFormatting (
94
+ formatWatcher : FormatWatcher ,
95
+ ) = this .apply {
96
+ addTextChangedListener(formatWatcher)
97
+ setOnLongClickListener {
98
+ showSnackbarMessage(formatWatcher.rawInput, formatWatcher.currentInput)
99
+ true
100
+ }
57
101
}
58
102
59
103
private fun getStringResource (@StringRes id : Int ): String = resources.getString(id)
104
+
105
+ private fun showSnackbarMessage (rawInput : String , formattedInput : String ) =
106
+ Snackbar .make(rootContainer,
107
+ " Raw input is ${rawInput.ifEmpty { " empty" }} ; the length is ${rawInput.length} \n " +
108
+ " Formatted input is ${formattedInput.ifEmpty { " empty" }} ; the length is ${formattedInput.length} " ,
109
+ Snackbar .LENGTH_SHORT ).show()
60
110
}
0 commit comments