0
我需要在用戶輸入編輯文本的每組數字後添加$。例如,一旦用戶輸入一個數字,$應該自動放在該數字的前面。我試圖使用textwatcher當我得到當前字符串,然後添加一個$並設置edittext的文本。當我這樣做時,鍵盤會凍結。在edittext中的每個單詞之後添加字符
我需要在用戶輸入編輯文本的每組數字後添加$。例如,一旦用戶輸入一個數字,$應該自動放在該數字的前面。我試圖使用textwatcher當我得到當前字符串,然後添加一個$並設置edittext的文本。當我這樣做時,鍵盤會凍結。在edittext中的每個單詞之後添加字符
聽起來好像你在textwatcher
的onTextChanged
方法中輸入了一個無限循環。你需要先刪除該偵聽器,然後進行更改,然後重新添加監聽器:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Remove TextChangedlistener so that this function does not
// enter an infinite loop when we change it in code!
yourEditText.removeTextChangedListener(this);
// Add your code to insert the '$' symbol here
// Re-add listener
yourEditText.addTextChangedListener(this);
// This just moves the cursor back to the end of the input
yourEditText.setSelection(editText.getText().length());
}
Impement一個[textwatcher(http://developer.android.com/reference/android/text/TextWatcher。 html) – 2014-11-21 00:20:16
你能發佈textwatcher的代碼嗎?一個常見的錯誤是對textwatcher內部的EditText文本進行更改,這會觸發'onTextChanged',它會再次更改文本等。等等。您可能已經進入循環。您應該首先刪除文本觀察器'yourEditText.removeTextChangedListener(this);',然後進行更改,然後將textwatcher添加回'yourEditText.addTextChangedListener(this);' - 全部在'onTextChanged'方法內。 – CodeMonkey 2014-11-21 00:51:12