我目前有一個使用排序計時器的應用程序,我有一個編輯文本可以改變時間。我想添加一個功能,在輸入特定數量的字符後自動插入冒號或句點,因爲更改時間的語法是特定的(它必須是XX:XX.X)在輸入時更改EditText中的字符? Android
我已經一直在玩它,它只會給我一個錯誤,當我嘗試改變editText文本。當我嘗試輸入時出現錯誤。
我複製並粘貼一些代碼,我在這裏找到,並改變了它適合我的需要/
editClock = (EditText) findViewById(R.id.editClock);
editClock.addTextChangedListener(new TextWatcher(){
int count =1 ;
@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(count==2)
editClock.setText(editable.toString()+":");
if(count==4)
editClock.setText(editable.toString()+".");
count++;
}
});
這是錯誤我得到:
12-07 20:34:08.380: E/AndroidRuntime(13549): FATAL EXCEPTION: main
12-07 20:34:08.380: E/AndroidRuntime(13549): java.lang.StackOverflowError
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidtext.TextUtils.getChars(TextUtils.java:77)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidtext.TextUtils.indexOf(TextUtils.java:110)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidtext.StaticLayout.generate(StaticLayout.java:182)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidtext.DynamicLayout.reflow(DynamicLayout.java:284)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidtext.DynamicLayout.<init>(DynamicLayout.java:170)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.makeSingleLayout(TextView.java:6058)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.makeNewLayout(TextView.java:5956)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.checkForRelayout(TextView.java:6495)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3771)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549): at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549): at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549): at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549): at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549): at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549): at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549): at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549): at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3604)
12-07 20:34:08.380: E/AndroidRuntime(13549): at com.example.timeonice.MainActivity$2.afterTextChanged(MainActivity.java:149)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.sendAfterTextChanged(TextView.java:7334)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3778)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.TextView.setText(TextView.java:3629)
12-07 20:34:08.380: E/AndroidRuntime(13549): at androidwidget.EditText.setText(EditText.java:80)
12-07 20:34:08.380: E/AndroidRuntime(13549): at an
錯誤是什麼?使用TextChangedListener時需要注意的一件事是遞歸 - 當你設置文本時,確保你沒有觸發另一個onTextChanged()。 –
增加了logcat,不知道這是否有幫助 – user3078867
是的,這有幫助。你正在獲得無限遞歸。 –