Numberpicker Doesn't Work With Keyboard
Solution 1:
If anyone wants a simple workaround, just add the below code before saving the value.
numberPicker.clearFocus();
Solution 2:
The NumberPicker
wasn't designed for that interaction. When you change the value with the keyboard you're making changes directly to a widget from the NumberPicker
and the widget itself doesn't see this change so this is why you end up with the last stored value. To get around this you'll need some hacky code, which isn't really recommended, because you'll need to access the underlying EditText
(assuming that the phone maker didn't changed this in the first place):
private EditText findInput(ViewGroup np) {
intcount= np.getChildCount();
for (inti=0; i < count; i++) {
finalViewchild= np.getChildAt(i);
if (child instanceof ViewGroup) {
findInput((ViewGroup) child);
} elseif (child instanceof EditText) {
return (EditText) child;
}
}
returnnull;
}
and then you'll use this code to set a TextWatcher
on the found EditText
to see when it's manually modified(and let the NumberPicker
know about this change):
EditTextinput= findInput(np);
TextWatchertw=newTextWatcher() {
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before,
int count) {}
@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
int after) {}
@OverridepublicvoidafterTextChanged(Editable s) {
if (s.toString().length() != 0) {
Integervalue= Integer.parseInt(s.toString());
if (value >= np.getMinValue()) {
np.setValue(value);
}
}
}
};
input.addTextChangedListener(tw);
Another option would be to make your own implementation of the NumberPicker
widget and insert the functionality you target.
Solution 3:
I see that you already have an answer, however this may be useful to others. You can extend android.widget.NumberPicker and make your own where you can configure the EditText. As you type on the soft keyboard, the value of your NumberPicker is immediately set to the value you're typing.
publicclassMyNumberPickerextendsandroid.widget.NumberPicker {
private EditText eText;
publicMyNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
}
@OverridepublicvoidaddView(View child) {
super.addView(child);
initEditText(child);
}
@OverridepublicvoidaddView(View child, int index, android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
initEditText(child);
}
@OverridepublicvoidaddView(View child, android.view.ViewGroup.LayoutParams params) {
super.addView(child, params);
initEditText(child);
}
publicvoidinitEditText(View view){
if(view instanceof EditText){
EditTexteText= (EditText) view;
eText.addTextChangedListener(newTextWatcher(){
@OverridepublicvoidafterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@OverridepublicvoidbeforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
try{
intnum= Integer.parseInt(s.toString());
setValue(num); //this line changes the value of your numberpicker
}catch(NumberFormatException E){
//do your catching here
}
}});
}
}
}
In your XML-file you put the following:
<com.mypackage.MyNumberPicker
android:id="@+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Solution 4:
You have to press the Done
button for the value to programmatically change.
Solution 5:
NumberPicker np = (NumberPicker) findViewById(R.id.numberPicker1);
String[] nums = newString[20];
for(int i=0; i<nums.length; i++)
nums[i] = Integer.toString(i);
np.setMinValue(1);
np.setMaxValue(20);
np.setWrapSelectorWheel(false);
np.setDisplayedValues(nums);
np.setValue(1);
Post a Comment for "Numberpicker Doesn't Work With Keyboard"