2012-08-01 82 views
1

我有這個問題,我怎麼才能格式化edittext上的顯示,我鍵入它?例如,如果我輸入5,它將格式化所需的貨幣。我可以在edittext中格式化文本,而onFocusChangeListener沒有問題,但客戶端需要onchange。android EditText格式,因爲我輸入

這裏有一些代碼:具有計算器錯誤

<code> 
    08-01 22:37:32.724: ERROR/AndroidRuntime(1566): FATAL EXCEPTION: main 
    java.lang.StackOverflowError 
    at android.view.View.addFocusables(View.java:4311) 
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731) 
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731) 
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731) 
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731) 
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731) 
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731) 
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731) 
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731) 
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731) 
    at android.view.ViewGroup.addFocusables(ViewGroup.java:712) 
    at android.view.View.getFocusables(View.java:4269) 
    at android.view.FocusFinder.findNextFocus(FocusFinder.java:114) 
    at android.view.FocusFinder.findNextFocus(FocusFinder.java:98) 
    at android.view.ViewGroup.focusSearch(ViewGroup.java:570) 
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572) 
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572) 
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572) 
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572) 
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572) 
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572) 
    at android.view.View.focusSearch(View.java:4192) 
    at android.widget.TextView.onCreateInputConnection(TextView.java:4936) 
    at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:964) 
    at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:919) 

    </code> 

任何想法

<code> 
    inputText = (EditText) findViewById(R.id.et_compare_rate_saving); 
    inputText.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)    { 
      //System.out.println("beforeTextChanged::: => "+charSequence); 
     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
      //System.out.println("onTextChanged::: => "+charSequence); 
     } 

     @Override 
     public void afterTextChanged(Editable editable) { 
      if(!editable.toString().isEmpty()){ 
       String amount = LO.removeSeparators(editable.toString(), getLocaleFormat()); 
       String newFormat = LO.compareRatesFormatting(Double.valueOf(amount),getLocaleFormat()); 
       System.out.println("afterTextChanged::: => "+newFormat); 
       inputText.setText(newFormat); 
      } 
      //System.out.println("afterTextChanged::: => "+editable.toString()); 
     } 
    }); 

    </code> 

IM? 歡呼聲。 非常感謝。

回答

0

你處於一個無限循環,這就是爲什麼你得到了stackoverflow。每次文本更改時,您的afterTextChanged被調用,這將更改文本並導致它被再次調用

2

爲了更清楚地瞭解情況,TextWatcher的文檔明確指出在更改文本時應小心afterTextChanged:

公共抽象無效afterTextChanged(編輯S)

這種方法被調用來通知您,S中的某處 ,文本已被更改。從此回調中進一步對 進行更改是合理的,但請注意不要讓自己的 陷入無限循環,因爲您所做的任何更改都會導致此方法以遞歸方式再次調用。(你不知道 發生了什麼變化,因爲其他的afterTextChanged()方法可能已經做了其他的更改並且使得偏移無效。但是如果你需要 來知道這裏,你可以使用setSpan(Object,int,int, 。INT)在 onTextChanged(CharSequence的,INT,INT,INT)來標記你的地方,然後 從這裏那裏的跨度結束了查找

+0

謝謝回覆...我覺得此http://機器人-sdk.appspot.com/reference/android/telephony/PhoneNumberFormattingTextWatcher.html這是一個實用工具,用於在文本更改後觀看textview。是否可以在edittext本身上執行類似的操作?謝謝 – 2012-08-04 04:27:46

+0

我覺得這篇文章有用http: //堆棧overflow.com/questions/5107901/better-way-to-format-currency-input-edittext – 2012-08-06 12:49:27