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:08

In my situation, I needed the view to be 6 digits wide. android:ems made the field too wide (since the M-character is wider than a digit).

I ended up using a transparent hint which dictates the minimum width of the field. The example below is six digits wide.

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="000000"
        android:textColorHint="#00FFFFFF"
        android:maxLength="6"
        android:inputType="numberPassword" />

This solution is independent of font, letter spacing and other things which may vary. The textColorHint is a 0% opaque white color, so the hint should never be visible to the user.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-13 13:18

You can also try using. . .

android:ems="4"
android:maxEms="4"
android:layout_width="wrap_content"

. . . this should insure the tightest fit possibly allowed by the default android edittext style

For additional control with the padding on both left and right you can use

android:background="#ffffff"
android:paddingLeft="0dp"
android:paddingRight="0dp"
查看更多
Explosion°爆炸
4楼-- · 2019-01-13 13:21

A little hackish, but you can put your EditText inside a FrameLayout alongside with another TextView with text="0000" and visibility=invisible.

FrameLayout Should have width=wrap_content and your EditText will have width=match_parent.
That should make it the right width always.

The advantage of this is, it's 100% xml-side, no code involved.

<FrameLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="0000"
    android:visibility="invisible" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:maxLength="4" />
</FrameLayout>
查看更多
劫难
5楼-- · 2019-01-13 13:22

Don't know about built-in solution but you can try passivly check the length and rise a dialog with error message at the moment of submission.

查看更多
你好瞎i
6楼-- · 2019-01-13 13:24

Use android:width="XXX.Xdp"

This lets you set the width in density pixels which ought to be relative to the screen density. I tried using ems based for a field 2 chars wide, but since ems requires a int, 2 was too small and 3 was too big. Blech.

查看更多
祖国的老花朵
7楼-- · 2019-01-13 13:24

There is an incredibly easy way to do this programmatically.

myEditText.setLayoutParams(new LinearLayout.LayoutParams(200, 50));

This would set the width to 200 and the height to 50!!

Enjoy :)

查看更多
登录 后发表回答