16
16
17
17
package net .margaritov .preference .colorpicker ;
18
18
19
+ import java .util .Locale ;
20
+
19
21
import android .app .Dialog ;
20
22
import android .content .Context ;
21
23
import android .content .res .ColorStateList ;
22
24
import android .graphics .Color ;
23
25
import android .graphics .PixelFormat ;
24
26
import android .os .Bundle ;
25
- import android .text .Editable ;
26
27
import android .text .InputFilter ;
27
- import android .text .TextWatcher ;
28
+ import android .text .InputType ;
29
+ import android .view .KeyEvent ;
28
30
import android .view .LayoutInflater ;
29
31
import android .view .View ;
32
+ import android .view .inputmethod .EditorInfo ;
33
+ import android .view .inputmethod .InputMethodManager ;
30
34
import android .widget .EditText ;
31
35
import android .widget .LinearLayout ;
36
+ import android .widget .TextView ;
32
37
33
38
public class ColorPickerDialog
34
39
extends
@@ -43,7 +48,6 @@ public class ColorPickerDialog
43
48
private ColorPickerPanelView mNewColor ;
44
49
45
50
private EditText mHexVal ;
46
- private boolean mHexInternalTextChange ;
47
51
private boolean mHexValueEnabled = false ;
48
52
private ColorStateList mHexDefaultTextColor ;
49
53
@@ -82,28 +86,31 @@ private void setUp(int color) {
82
86
mNewColor = (ColorPickerPanelView ) layout .findViewById (R .id .new_color_panel );
83
87
84
88
mHexVal = (EditText ) layout .findViewById (R .id .hex_val );
89
+ mHexVal .setInputType (InputType .TYPE_TEXT_FLAG_NO_SUGGESTIONS );
85
90
mHexDefaultTextColor = mHexVal .getTextColors ();
86
- mHexVal .addTextChangedListener (new TextWatcher () {
87
- @ Override
88
- public void onTextChanged (CharSequence s , int start , int before , int count ) {}
89
- @ Override
90
- public void beforeTextChanged (CharSequence s , int start , int count , int after ) {}
91
+
92
+ mHexVal .setOnEditorActionListener (new TextView .OnEditorActionListener () {
93
+
91
94
@ Override
92
- public void afterTextChanged (Editable s ) {
93
- if (mHexValueEnabled ) {
94
- if (mHexInternalTextChange ) return ;
95
-
95
+ public boolean onEditorAction (TextView v , int actionId , KeyEvent event ) {
96
+ if (actionId == EditorInfo .IME_ACTION_DONE ) {
97
+ InputMethodManager imm = (InputMethodManager ) v .getContext ().getSystemService (Context .INPUT_METHOD_SERVICE );
98
+ imm .hideSoftInputFromWindow (v .getWindowToken (), 0 );
99
+ String s = mHexVal .getText ().toString ();
96
100
if (s .length () > 5 || s .length () < 10 ) {
97
101
try {
98
102
int c = ColorPickerPreference .convertToColorInt (s .toString ());
99
103
mColorPicker .setColor (c , true );
100
104
mHexVal .setTextColor (mHexDefaultTextColor );
101
- } catch (NumberFormatException e ) {
105
+ } catch (IllegalArgumentException e ) {
102
106
mHexVal .setTextColor (Color .RED );
103
107
}
104
- } else
108
+ } else {
105
109
mHexVal .setTextColor (Color .RED );
110
+ }
111
+ return true ;
106
112
}
113
+ return false ;
107
114
}
108
115
});
109
116
@@ -161,12 +168,12 @@ private void updateHexLengthFilter() {
161
168
}
162
169
163
170
private void updateHexValue (int color ) {
164
- mHexInternalTextChange = true ;
165
- if ( getAlphaSliderVisible ())
166
- mHexVal . setText ( ColorPickerPreference . convertToARGB ( color ));
167
- else
168
- mHexVal . setText ( ColorPickerPreference . convertToRGB ( color ));
169
- mHexInternalTextChange = false ;
171
+ if ( getAlphaSliderVisible ()) {
172
+ mHexVal . setText ( ColorPickerPreference . convertToARGB ( color ). toUpperCase ( Locale . getDefault ()));
173
+ } else {
174
+ mHexVal . setText ( ColorPickerPreference . convertToRGB ( color ). toUpperCase ( Locale . getDefault ()));
175
+ }
176
+ mHexVal . setTextColor ( mHexDefaultTextColor ) ;
170
177
}
171
178
172
179
public void setAlphaSliderVisible (boolean visible ) {
0 commit comments