2010-09-11 40 views
11

我抄Android的NumberPicker小工具,我自己的應用程序,但我有一個問題...的Android NumberPicker不節能的EditText改變

當有人手動點擊EditText上,並通過鍵盤改變了它的選擇沒有保存。是否有一些可以實現的偵聽器來檢查用戶是否手動更改EditText以將其設置爲最新?或者其他的東西?

回答

0

你可以嘗試將TextWatcher設爲您的EditText做同樣或不允許對用戶輸入的值手動那裏...

或維持OK按鈕那裏,並從取出來的值僅在用戶單擊確定按鈕時才能使用EditText

您試過這個示例嗎?

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

+0

是的,這正是我如何複製NumberPicker的方法。不幸的是,我希望用戶能夠手動輸入他們的分數。 – 2010-09-12 14:33:06

+0

你有沒有嘗試設置一個文本觀察器到你的EditText ..?檢查這篇文章http://stackoverflow.com/questions/3665470/measure-length-of-edittext-string-dynamically-in-android/3666012#3666012 – DeRagan 2010-09-12 15:12:19

0

不知道,如果你使用的是相同的Numberpicker礦山:http://www.quietlycoding.com/?p=5。我試圖添加一個OnEditorActionListener到NumberClass,似乎它解決了我的問題。

  • 修改NumberPicker.java讓它擴展OnEditorActionListener。
  • add mText.setOnEditorActionListener(this);到NumberPicker構造
  • 實施onEditorAction

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

希望能有所幫助。

1

我使用numberpicker http://www.quietlycoding.com/?p=5也有同樣的問題,我通過將OnKeyListener(而不是Junzi已經建議的OnEditorActionListener)添加到NumberPicker類來解決它。其他的步驟是一樣的,所以:

  1. 讓NumberPicker.java延長OnKeyListener
  2. 添加mText.setOnKeyListener(本);到NumberPicker構造
  3. 實施安其:


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

我用邁克爾·諾瓦克數字選擇器。雖然onEditorAction已經實現,但沒有正確設置輸入。簡單的解決方法是在getCurrent中的textView上調用validateInput。

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

如果從Android4.0.x使用的是Android NumberPicker意味着你可以使用的getValue得到NumberPicker的價值()。 http://developer.android.com/reference/android/widget/NumberPicker.html#getValue()

這是一個簡單的方法來獲得Numberpicker在android系統4.0.x版...

final NumberPicker np = new NumberPicker(CustomizedListView.this); np.setMinValue(0); np.setMaxValue(100); np.setWrapSelectorWheel(true); ,你可以得到使用np.getValue()方法的值。

36

我發現這個解決方案在Android的開發商谷歌組某處遺失:

的Android SDK NumberPicker小部件,只需使用:

myNumberPicker.clearFocus(); 

試圖獲取其值之前。 當你只有NumberPicker的活動,也許一個按鈕或微調,並且你編輯它並嘗試點擊其他地方,它的onFocusChangedListener處理不當。所以你只需要在使用getValue()之前強制它失去焦點。像我的魅力一樣工作。

+0

+1,爲我工作 – 66CLSjY 2013-04-26 14:17:32

+0

偉大...爲我工作。 – AB1209 2013-09-26 05:51:02

+0

使我的一天...謝謝。 – hotzen 2014-09-07 20:55:34