限制EditText文本长度

时间:2013-09-25 15:00:52

标签: android android-edittext

我试图这样做但是我得到了#34; Stack Overflow错误"所以我接受了建议并来到这里寻求帮助。

我想限制用户可以插入的文本的长度,因此当他键入长度限制以上的字符时,字符将被删除

sms_input.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {

            String cs = sms_input.getText().toString();
            if (cs.length() > 160) { sms_input.setText(cs.substring(0, MAX_MSG_LENGTH + 1)); }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }

    });

logcat的:

09-25 17:07:34.972: E/AndroidRuntime(22733): FATAL EXCEPTION: main

09-25 17:07:34.972:E / AndroidRuntime(22733):java.lang.StackOverflowError 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.graphics.Paint.getTextRunAdvances(Paint.java:1754) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.graphics.Paint.getTextRunAdvances(Paint.java:1720) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.text.MeasuredText.addStyleRun(MeasuredText.java:164) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.text.MeasuredText.addStyleRun(MeasuredText.java:204) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.text.StaticLayout.generate(StaticLayout.java:281) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.text.DynamicLayout.reflow(DynamicLayout.java:284) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.text.DynamicLayout。(DynamicLayout.java:170) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.makeSingleLayout(TextView.java:5986) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.makeNewLayout(TextView.java:5884) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.checkForRelayout(TextView.java:6423) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3696) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972:E / AndroidRuntime(22733):at com.easyray.lemon.SmsChat $ 5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972:E / AndroidRuntime(22733):at com.easyray.lemon.SmsChat $ 5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972:E / AndroidRuntime(22733):at com.easyray.lemon.SmsChat $ 5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972:E / AndroidRuntime(22733):at com.easyray.lemon.SmsChat $ 5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972:E / AndroidRuntime(22733):at com.easyray.lemon.SmsChat $ 5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972:E / AndroidRuntime(22733):at com.easyray.lemon.SmsChat $ 5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972:E / AndroidRuntime(22733):at com.easyray.lemon.SmsChat $ 5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972:E / AndroidRuntime(22733):at com.easyray.lemon.SmsChat $ 5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.EditText.setText(EditText.java:80) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3529) 09-25 17:07:34.972:E / AndroidRuntime(22733):at com.easyray.lemon.SmsChat $ 5.afterTextChanged(SmsChat.java:211) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.sendAfterTextChanged(TextView.java:7247) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3703) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.widget.TextView.setText(TextView.java:3554) 09-25 17:07:34.972:E / AndroidRuntime(22733):在android.w

2 个答案:

答案 0 :(得分:4)

你不想做所有这些。只需在XML中尝试这个

android:maxLength="120"

当您尝试输入超过120个字符时,它只会阻止您输入下一个字符。

我认为这就是你要找的东西。如果没有,请留下。

答案 1 :(得分:3)

之前已经回答了同样的问题,例如这里(TextWatcher afterTextChanged causes stackoverflow in android

但基本上发生的是你更改方法“afterTextChanged”中的文本并触发再次调用的方法,从而产生以StackOverflow异常结束的无限循环:)