Android: EditText maxLength being ignored

試著忘記壹切 提交于 2019-12-08 02:51:25

I would make sure that your XML attributes are not effecting another one.

It seems like these 2 could be the issue

    android:maxLength="4"
    android:singleLine="true"

I see you solved it already! OK, the issue was...

    android:singleLine="true"

But you should double check to make sure that you cannot create a new line!

You could also use the underrated LengthFilter:

EditText editText = findViewById(R.id.edit_text);
int length = 4;
editText.setFilters(new android.text.InputFilter[] { new android.text.InputFilter.LengthFilter(length) });

You need to remove below line in your layout to solve this problem

android:singleLine="true"

Apart from that, you can always inflate the EditText dynamically with custom view like below and you can register for textwatcher in input mode.

LayoutInflator mInflator = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
View view = mInflator.inflate(R.layout.dialog_question_new, null);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(context, R.style.CustomDialogTheme);
EditText input = ( EditText ) view.findViewById( R.id.dialog_input_question_new); 
mBuilder.setCustomTitle(view);
mBuilder.setPositiveButton(...
mBuilder.setPositiveButton(...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!