在Android中,如何创建固定宽度的EditText?

时间:2021-12-25 14:59:07

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.)

我想创建一个EditText,它只接受数字,最多有4个数字,并且永远不会比填充4个数字时更窄(空时不缩小,或者必须扩展以容纳4个数字,所以它是固定。)

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

前两个完成:android:inputType =“number”和android:maxLength =“4”。

How can I set the minimum length?

如何设置最小长度?

10 个解决方案

#1


35  

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.

有点hackish,但你可以把你的EditText放在FrameLayout中,另一个TextView与text =“0000”和visibility = invisible。 FrameLayout应该有width = wrap_content,你的EditText将有width = match_parent。这应该使它始终是正确的宽度。

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

这样做的好处是,它是100%xml端,不涉及代码。

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

#2


93  

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

您可以使用的EditText的一个很少使用(AFAIK)属性是:

android:ems="4"

机器人: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.

这使EditText正好宽4埃。 em宽度是所选字体中最宽(M)字符的宽度。

#3


18  

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).

在我的情况下,我需要视图宽6位数。 android:ems使得该字段太宽(因为M字符比数字宽)。

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.

此解决方案与字体,字母间距和其他可能不同的内容无关。 textColorHint是0%不透明的白色,因此提示永远不会对用户可见。

#4


8  

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

。 。 。这应该确保默认的android edittext样式可能允许的最紧密的匹配

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

对于左右两侧填充的附加控制,您可以使用

android:background="#ffffff"
android:paddingLeft="0dp"
android:paddingRight="0dp"

#5


2  

Use android:width="XXX.Xdp"

使用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.

这使您可以设置密度像素的宽度,该像素应该相对于屏幕密度。我尝试使用基于字段2字符宽的ems,但由于ems需要int,2太小而3太大。布莱什。

#6


1  

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!!

这会将宽度设置为200,将高度设置为50!

Enjoy :)

请享用 :)

#7


0  

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.

不知道内置解决方案,但你可以尝试钝性检查长度,并在提交时出现一个错误消息对话框。

#8


0  

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.

问题是,由于触摸另一个敏感视图,菜单按钮或(硬键盘光标控制)等许多不同的操作,EditText小部件可能会失去焦点。输入的最大长度或类型的取消可以在连接过程中完成(如果字符通过过滤器或不超过长度,则接受该字符)。但焦点丢失可能每次都会出现 - 即使提交尚未完成,输入也很短 - >因此没有内置的最小长度解决方案,因为应用程序此时会阻止执行另一个事件或任务。至少在进入工作流程的下一步之前,您必须验证输入。

#9


0  

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

我使用android:layout_width =“60dip”所以同意迈克弗拉托。

The ems solution appears to be interesting as well.

ems解决方案似乎也很有趣。

Phil

菲尔

#10


0  

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

当你将字符放在wditText宽度以上而不是它的增加宽度或EditText宽度时,你可以设置其他方式。请设置上面视图的LeftOf和toRightOf来管理这些事情。只有一个解决方案

#1


35  

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.

有点hackish,但你可以把你的EditText放在FrameLayout中,另一个TextView与text =“0000”和visibility = invisible。 FrameLayout应该有width = wrap_content,你的EditText将有width = match_parent。这应该使它始终是正确的宽度。

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

这样做的好处是,它是100%xml端,不涉及代码。

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

#2


93  

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

您可以使用的EditText的一个很少使用(AFAIK)属性是:

android:ems="4"

机器人: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.

这使EditText正好宽4埃。 em宽度是所选字体中最宽(M)字符的宽度。

#3


18  

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).

在我的情况下,我需要视图宽6位数。 android:ems使得该字段太宽(因为M字符比数字宽)。

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.

此解决方案与字体,字母间距和其他可能不同的内容无关。 textColorHint是0%不透明的白色,因此提示永远不会对用户可见。

#4


8  

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

。 。 。这应该确保默认的android edittext样式可能允许的最紧密的匹配

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

对于左右两侧填充的附加控制,您可以使用

android:background="#ffffff"
android:paddingLeft="0dp"
android:paddingRight="0dp"

#5


2  

Use android:width="XXX.Xdp"

使用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.

这使您可以设置密度像素的宽度,该像素应该相对于屏幕密度。我尝试使用基于字段2字符宽的ems,但由于ems需要int,2太小而3太大。布莱什。

#6


1  

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!!

这会将宽度设置为200,将高度设置为50!

Enjoy :)

请享用 :)

#7


0  

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.

不知道内置解决方案,但你可以尝试钝性检查长度,并在提交时出现一个错误消息对话框。

#8


0  

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.

问题是,由于触摸另一个敏感视图,菜单按钮或(硬键盘光标控制)等许多不同的操作,EditText小部件可能会失去焦点。输入的最大长度或类型的取消可以在连接过程中完成(如果字符通过过滤器或不超过长度,则接受该字符)。但焦点丢失可能每次都会出现 - 即使提交尚未完成,输入也很短 - >因此没有内置的最小长度解决方案,因为应用程序此时会阻止执行另一个事件或任务。至少在进入工作流程的下一步之前,您必须验证输入。

#9


0  

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

我使用android:layout_width =“60dip”所以同意迈克弗拉托。

The ems solution appears to be interesting as well.

ems解决方案似乎也很有趣。

Phil

菲尔

#10


0  

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

当你将字符放在wditText宽度以上而不是它的增加宽度或EditText宽度时,你可以设置其他方式。请设置上面视图的LeftOf和toRightOf来管理这些事情。只有一个解决方案