20
20
*/
21
21
public class MaterialTextField extends FrameLayout {
22
22
23
-
24
23
protected TextView label ;
25
24
protected View card ;
26
25
protected ImageView image ;
@@ -35,6 +34,7 @@ public class MaterialTextField extends FrameLayout {
35
34
protected int labelColor = -1 ;
36
35
protected int imageDrawableId = -1 ;
37
36
protected int cardCollapsedHeight = -1 ;
37
+ protected boolean hasFocus = false ;
38
38
39
39
protected float reducedScale = 0.2f ;
40
40
@@ -44,7 +44,7 @@ public MaterialTextField(Context context) {
44
44
45
45
public MaterialTextField (Context context , AttributeSet attrs ) {
46
46
super (context , attrs );
47
- handleAttributes (context ,attrs );
47
+ handleAttributes (context , attrs );
48
48
}
49
49
50
50
public MaterialTextField (Context context , AttributeSet attrs , int defStyleAttr ) {
@@ -93,7 +93,6 @@ public void onAnimationUpdate(View view) {
93
93
.scaleY (reducedScale )
94
94
.setDuration (ANIMATION_DURATION );
95
95
96
-
97
96
if (OPEN_KEYBOARD_ON_FOCUS ) {
98
97
((InputMethodManager ) getContext ().getSystemService (Context .INPUT_METHOD_SERVICE )).toggleSoftInput (InputMethodManager .HIDE_IMPLICIT_ONLY , 0 );
99
98
}
@@ -127,7 +126,9 @@ public void expand() {
127
126
.scaleY (1f )
128
127
.setDuration (ANIMATION_DURATION );
129
128
130
- editText .requestFocus ();
129
+ if (editText != null ) {
130
+ editText .requestFocus ();
131
+ }
131
132
if (OPEN_KEYBOARD_ON_FOCUS ) {
132
133
((InputMethodManager ) getContext ().getSystemService (Context .INPUT_METHOD_SERVICE )).showSoftInput (editText , InputMethodManager .SHOW_IMPLICIT );
133
134
}
@@ -160,6 +161,33 @@ public boolean isExpanded() {
160
161
return expanded ;
161
162
}
162
163
164
+ public void setHasFocus (boolean hasFocus ) {
165
+ this .hasFocus = hasFocus ;
166
+ final InputMethodManager inputMethodManager = (InputMethodManager ) getContext ().getSystemService (Context .INPUT_METHOD_SERVICE );
167
+ if (hasFocus ) {
168
+ expand ();
169
+ editText .postDelayed (new Runnable () {
170
+ public void run () {
171
+ editText .requestFocusFromTouch ();
172
+ inputMethodManager .showSoftInput (editText , 0 );
173
+ }
174
+ }, 300 );
175
+ } else {
176
+ reduce ();
177
+ }
178
+ }
179
+
180
+ @ Override
181
+ public void requestChildFocus (View child , View focused ) {
182
+ super .requestChildFocus (child , focused );
183
+
184
+ if (focused != null ) {
185
+ setHasFocus (true );
186
+ } else {
187
+ setHasFocus (false );
188
+ }
189
+ }
190
+
163
191
protected void handleAttributes (Context context , AttributeSet attrs ) {
164
192
try {
165
193
TypedArray styledAttrs = context .obtainStyledAttributes (attrs , R .styleable .MaterialTextField );
@@ -179,6 +207,9 @@ protected void handleAttributes(Context context, AttributeSet attrs) {
179
207
{
180
208
cardCollapsedHeight = styledAttrs .getDimensionPixelOffset (R .styleable .MaterialTextField_mtf_cardCollapsedHeight , context .getResources ().getDimensionPixelOffset (R .dimen .mtf_cardHeight_initial ));
181
209
}
210
+ {
211
+ hasFocus = styledAttrs .getBoolean (R .styleable .MaterialTextField_mtf_hasFocus , false );
212
+ }
182
213
183
214
styledAttrs .recycle ();
184
215
} catch (Exception e ) {
@@ -245,6 +276,7 @@ public void onClick(View v) {
245
276
}
246
277
});
247
278
279
+ setHasFocus (hasFocus );
248
280
}
249
281
250
282
protected void customizeFromAttributes () {
0 commit comments