In Android, how to create EditText of fixed width?

后端 未结 10 434
醉酒成梦
醉酒成梦 2020-12-04 17:52

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

相关标签:
10条回答
  • 2020-12-04 18:25

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

    The ems solution appears to be interesting as well.

    Phil

    0 讨论(0)
  • 2020-12-04 18: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

    0 讨论(0)
  • 2020-12-04 18:32

    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.

    0 讨论(0)
  • 2020-12-04 18:33

    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.

    0 讨论(0)
提交回复
热议问题