在Android中,如何创建固定宽度的EditText?
我想创建一个只接受数字的 EditText,最多有 4 个数字,并且永远不会比填充 4 个数字时更窄(空时不会缩小,或者必须扩展以容纳 4 个数字,所以它是已修复。)
前 2 个是通过以下方式完成的: android:inputType="number" 和 android:maxLength="4"。
如何设置最小长度?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以使用的 EditText 的一个很少使用的 (AFAIK) 属性是:
android:ems="4"
这使得 EditText 的宽度恰好为 4 ems。 em 宽度是所选字体中最宽 (M) 字符的宽度。
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.
有点hackish,但您可以将
EditText
与另一个带有text 的
和TextView
一起放入FrameLayout
中="0000"visibility=invisible.
FrameLayout
应该有width=wrap_content
和您的EditText
将具有width=match_parent
。这应该使其始终具有正确的宽度。
这样做的优点是,它是 100% xml 端,不涉及任何代码。
A little hackish, but you can put your
EditText
inside aFrameLayout
alongside with anotherTextView
withtext="0000"
andvisibility=invisible.
FrameLayout
Should havewidth=wrap_content
and yourEditText
will havewidth=match_parent
.That should make it the right width always.
The advantage of this is, it's 100% xml-side, no code involved.
在我的情况下,我需要视图为 6 位数宽。 android:ems 使字段太宽(因为 M 字符比数字宽)。
我最终使用了一个透明的提示,它规定了字段的最小宽度。下面的示例是六位数宽。
该解决方案独立于字体、字母间距和其他可能变化的因素。 textColorHint 是 0% 不透明的白色,因此用户永远不应该看到提示。
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.
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.
您也可以尝试使用。 。 。
。 。 。这应该确保默认 android edittext 样式可能允许的最紧密配合
对于左右两侧填充的附加控制,您可以使用
You can also try using. . .
. . . 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:width="XXX.Xdp"
这可以让您设置密度像素的宽度,该宽度应该相对于屏幕密度。我尝试使用基于 ems 的 2 个字符宽的字段,但由于 ems 需要一个 int,所以 2 太小,3 太大。布莱赫。
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.
有一种非常简单的方法可以通过编程来完成此操作。
这会将宽度设置为 200,高度设置为 50!
享受 :)
There is an incredibly easy way to do this programmatically.
This would set the width to 200 and the height to 50!!
Enjoy :)
不知道内置解决方案,但您可以尝试被动检查长度并在提交时弹出一个包含错误消息的对话框。
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.
问题是,EditText 小部件可能会因为许多不同的操作而失去焦点,例如触摸另一个敏感视图、菜单按钮或(硬键盘光标控制)等。可以在绑定过程中对输入的最大长度或类型进行限制(如果字符通过过滤器或未超过长度则接受该字符)。但每次都会发生焦点丢失的情况 - 即使提交尚未完成并且输入很短 ->因此,没有内置的最小长度解决方案,因为应用程序会在此时阻止另一个事件或任务的执行。
至少在进入工作流程的下一步之前,您必须验证您的输入。
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.
我使用 android:layout_width="60dip" 所以同意 mike fratto 的观点。
ems 解决方案似乎也很有趣。
菲尔
I use android:layout_width="60dip" so agree with mike fratto.
The ems solution appears to be interesting as well.
Phil
当您放置的字符超过 wditText 宽度而不是它的增加宽度或 EditText 宽度时,您可以以其他方式设置对齐方式。
请设置上面视图的 toLeftOf 和 toRightOf 来管理这些东西。只有一种解决方案
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
根据您的布局和背景,editText 中的边距是
仅用于 drawable 文件夹中 editText 的边框。
of margin in the editText as per your layout and the background is
just for the border of the editText in the drawable folder.