In Android, how to create EditText of fixed width?

2019-01-13 12:55发布

I would like to create an EditText which accepts only numbers, has a maximum of 4 numbers, and is never narrower than it would be if filled with 4 numbers (does not shrink when empty, or have to expand to accommodate 4 numbers, so it's fixed.)

The first 2 are accomplished with: android:inputType="number" and android:maxLength="4".

How can I set the minimum length?

10条回答
闹够了就滚
2楼-- · 2019-01-13 13:26

You can set alignment other wise when you put character more than wditText width than It's increase width or EditText width. Please set toLeftOf and toRightOf of the above view to manage this things. Only one solution

查看更多
做自己的国王
3楼-- · 2019-01-13 13:27

The problem is, that an EditText widget can loss focus because of a lot of different actions like touching another sensitive view, menu button or (hard keyboard cursor control) and so on. The restiction of a maximum length or type of the input can be done during the tying process (accept the character if it passes the filter or the length is not exceeded). but focus lost can be occour every time - even the submission is not completed yet and the input to short -> therefore there is no build-in minimum length solution, because the application would block at this point the execution of another event or task. At least you have to validate your input before going to the next step in your workflow.

查看更多
够拽才男人
4楼-- · 2019-01-13 13:27

I use android:layout_width="60dip" so agree with mike fratto.

The ems solution appears to be interesting as well.

Phil

查看更多
forever°为你锁心
5楼-- · 2019-01-13 13:32

One rarely-used (AFAIK) attribute of an EditText that you can use is:

android:ems="4"

This makes the EditText exactly 4 ems wide. An em width is the width of the widest (M) character in the selected font.

查看更多
登录 后发表回答