我目前有一个使用各种计时器的应用程序,我有一个可以改变时间的编辑文本。我想添加一个功能,在输入一定数量的字符后自动插入冒号或句点,因为更改时间的语法是特定的(它必须是XX:XX.X)
我一直在玩它,当我尝试更改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
答案 0 :(得分:2)
您可以解决的一种方法是在代码中添加类级别标志。当您要调用setText()并在之后清除它时设置标志。然后,在afterTextChanged()的开头,检查你的标志。像这样:
@Override
public void afterTextChanged(Editable editable) {
if (overridingText)
return;
if(count==2)
{
overridingText = true;
editClock.setText(editable.toString()+":");
overridingText = false;
}
else if(count==4)
{
overridingText = true;
editClock.setText(editable.toString()+".");
overridingText = false;
}
count++;
}
也许Google提供了更好的方法来实现这一目标。不过我不知道是不是。