开发者

Android NumberPicker not saving EditText changes

I have copied Android's NumberPicker widget to my own application, but I'm having one problem...

When someone manually clicks the EditText and changes it via the keyboard, the selection is not saved. Is ther开发者_如何学Pythone some listener that I can implement to check and see if the user manually changes the EditText to set it as current? Or something?


I found this solution lost somewhere in android-developers google group:

For android SDK NumberPicker widget, simply use:

myNumberPicker.clearFocus();

before you try to get its value. When you have an activity with only the NumberPicker and maybe a button or a spinner, and you edit it and try to click elsewhere, its onFocusChangedListener is not handled properly. So you just need to force it to lost focus before using getValue(). Worked like a charm to me.


I used the Michael Novak number picker. Although onEditorAction was already implemented it didn't properly set the input. The easy fix for me was to call validateInput on the textView in getCurrent.

/**
 * @return the current value.
 */
public int getCurrent() {
    validateInput(mText);
    return mCurrent;
}


I has same problem using numberpicker http://www.quietlycoding.com/?p=5 and I solved it by adding OnKeyListener (instead OnEditorActionListener which was already suggested by Junzi) to NumberPicker class. Other steps are same so:

  1. Let NumberPicker.java extend OnKeyListener
  2. add mText.setOnKeyListener(this); to NumberPicker constructor
  3. implement OnKey:


public boolean onKey(View v, int keyCode, KeyEvent event) {  
    validateInput(v);  
    return false;  
}


If you are using Android NumberPicker from Android4.0.x means you can get the Value of NumberPicker using the getValue(). http://developer.android.com/reference/android/widget/NumberPicker.html#getValue()

It's an Easy way to get the Numberpicker in android 4.0.x...

final NumberPicker np = new NumberPicker(CustomizedListView.this); np.setMinValue(0); np.setMaxValue(100); np.setWrapSelectorWheel(true); And you can get the value by using np.getValue() method.


You can try to set a TextWatcher to your EditText to do the same or do not allow an user to enter a value manually there...

Or keep an OK button there and take the values out from the EditText only when an user clicks the OK button

Did you try this sample ?

http://www.quietlycoding.com/?p=5


Not sure if you are using the same Numberpicker as mine: http://www.quietlycoding.com/?p=5. I have tried to add a OnEditorActionListener to the NumberClass, seems it solved the problem for me.

  • modify the NumberPicker.java let it extends OnEditorActionListener.
  • add mText.setOnEditorActionListener(this); to NumberPicker constructor
  • implement onEditorAction:

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        validateInput(v);
        return false;
    }
    

Hope it can be helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜