@@ -5,15 +5,16 @@ import androidx.activity.ComponentActivity
5
5
import androidx.activity.compose.setContent
6
6
import androidx.compose.foundation.BorderStroke
7
7
import androidx.compose.foundation.border
8
- import androidx.compose.foundation.layout.Column
9
- import androidx.compose.foundation.layout.Spacer
10
- import androidx.compose.foundation.layout.fillMaxSize
11
- import androidx.compose.foundation.layout.height
8
+ import androidx.compose.foundation.layout.*
12
9
import androidx.compose.foundation.shape.RoundedCornerShape
13
- import androidx.compose.material.MaterialTheme
14
- import androidx.compose.material.Surface
10
+ import androidx.compose.material.*
11
+ import androidx.compose.material.icons.Icons
12
+ import androidx.compose.material.icons.filled.ArrowBack
13
+ import androidx.compose.runtime.Composable
14
+ import androidx.compose.runtime.MutableState
15
15
import androidx.compose.runtime.mutableStateOf
16
16
import androidx.compose.runtime.remember
17
+ import androidx.compose.ui.Alignment
17
18
import androidx.compose.ui.Modifier
18
19
import androidx.compose.ui.graphics.Color
19
20
import androidx.compose.ui.unit.dp
@@ -27,9 +28,11 @@ class MainActivity : ComponentActivity() {
27
28
// A surface container using the 'background' color from the theme
28
29
Surface (color = MaterialTheme .colors.background) {
29
30
val text = remember { mutableStateOf(" " ) }
31
+ val length = 5 // Define the length of the PIN here
30
32
31
-
32
- Column (modifier = Modifier .fillMaxSize()) {
33
+ Column (modifier = Modifier .fillMaxSize(),
34
+ Arrangement .Center , Alignment .CenterHorizontally
35
+ ) {
33
36
34
37
Spacer (modifier = Modifier .height(20 .dp))
35
38
@@ -47,16 +50,126 @@ class MainActivity : ComponentActivity() {
47
50
shape = RoundedCornerShape (3 .dp)
48
51
), value = text.value,
49
52
obscureText = " *" ,
50
- length = 6
53
+ length = length, // Use the number defined here
54
+ disableKeypad = true , // Do not open the android keypad
51
55
) {
52
56
text.value = it
53
57
}
54
58
59
+ Spacer (modifier = Modifier .height(20 .dp))
60
+
61
+ KeyPad (input = text, length)
62
+
55
63
}
56
64
57
65
58
66
}
59
67
}
60
68
}
61
69
}
70
+
71
+ @Composable
72
+ fun KeyPad (input : MutableState <String >, length : Int ){
73
+ val callback = {
74
+ text: String -> handleButtonClick(text, input, length)
75
+ }
76
+
77
+ Column (
78
+ modifier = Modifier
79
+ .padding(5 .dp, 5 .dp)
80
+ .fillMaxWidth(0.5f )
81
+
82
+ ) {
83
+ NumKeypadRow (
84
+ listOf (" 1" , " 2" , " 3" ),
85
+ listOf (0.33f , 0.33f , 0.33f ),
86
+ callback
87
+ )
88
+ NumKeypadRow (
89
+ listOf (" 4" , " 5" , " 6" ),
90
+ listOf (0.33f , 0.33f , 0.33f ),
91
+ callback
92
+ )
93
+ NumKeypadRow (
94
+ listOf (" 7" , " 8" , " 9" ),
95
+ listOf (0.33f , 0.33f , 0.33f ),
96
+ callback
97
+ )
98
+ NumKeypadRow (
99
+ listOf (" C" , " 0" , " ←" ),
100
+ listOf (0.33f , 0.33f , 0.33f ),
101
+ callback
102
+ )
103
+ }
104
+ }
105
+
106
+ @Composable
107
+ fun NumKeypadRow (
108
+ texts : List <String >,
109
+ weights : List <Float >,
110
+ callback : (text: String ) -> Any
111
+ ) {
112
+ Row (
113
+ modifier = Modifier
114
+ .fillMaxWidth()
115
+ .padding(2 .dp, 2 .dp)
116
+ ) {
117
+ for (i in texts.indices) {
118
+ val useIcon = texts[i]== " ←"
119
+ MyButton (
120
+ text = texts[i],
121
+ callback = callback,
122
+ modifier = Modifier
123
+ .weight(weights[i]),
124
+ useIcon,
125
+ )
126
+ }
127
+ }
128
+ }
129
+
130
+ @Composable
131
+ fun MyButton (
132
+ text : String ,
133
+ callback : (text: String ) -> Any ,
134
+ modifier : Modifier = Modifier ,
135
+ useIcon : Boolean = false
136
+ ) {
137
+ Button (
138
+ colors = ButtonDefaults .textButtonColors(
139
+ backgroundColor = Color .LightGray ,
140
+ contentColor = Color .Black ,
141
+
142
+ ),
143
+ onClick = {
144
+ callback(text)
145
+ },
146
+ border = null ,
147
+ elevation = null ,
148
+ modifier = modifier
149
+ .size(40 .dp, 35 .dp)
150
+ .padding(2 .dp, 0 .dp)
151
+ ) {
152
+ if (useIcon) {
153
+ if (text == " ←" ) Icon (
154
+ Icons .Filled .ArrowBack ,
155
+ contentDescription = " Favorite" ,
156
+ modifier = Modifier .size(ButtonDefaults .IconSize )
157
+ )
158
+ }else Text (text)
159
+ }
160
+ }
161
+
162
+ private fun handleButtonClick (
163
+ txt : String ,
164
+ inputTextView : MutableState <String >,
165
+ length : Int
166
+ ) {
167
+ when (txt) {
168
+ " ←" -> if (inputTextView.value.isNotEmpty()) {
169
+ inputTextView.value = inputTextView.value.dropLast(1 ) // remove last char
170
+ }
171
+ " C" -> inputTextView.value = " " // clear all text
172
+ else -> if (inputTextView.value.length< length) inputTextView.value + = txt // handles key input here
173
+ }
174
+ }
62
175
}
0 commit comments