将 TextInputEditText 的宽度设置为 n 个字符
我正在尝试实现一种布局,用户可以在其中输入他们的电话号码。电话号码将为 11 位数字。所以我想构建一个固定宽度的文本输入字段,可以容纳 11 个字符。
注意:我不希望输入字段超过 11 个字符。假设每个字符的宽度为 10 像素。所以输入字段的宽度应该是 11*10px = 110px(我在这里没有考虑可绘制图标)。
这是我在 XML 中尝试过的:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_48"
app:startIconDrawable="@drawable/ic_phone">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="11"
android:inputType="phone"
android:maxLength="11"
android:maxLines="1"
android:minEms="11"
android:text="01"
android:textSize="20sp"
android:typeface="monospace" />
</com.google.android.material.textfield.TextInputLayout>
但这会产生如下所示的输出:
如您所见,尽管使用了 ems
和 minEms
,该字段为 2字符宽(如果我不添加 android:text
,则为 0 个字符宽)。但我希望它的宽度为 11 个字符(不多或更少)。为什么 ems 在这里不起作用以及解决方案是什么?
I'm trying to implement a layout where user will input their phone number. The phone number will be 11 digits. And so I wanted to build a text input field with a fixed width that can accommodate 11 characters.
NOTE: I don't want the input field to be wider than 11 characters. Let's say each character is 10px wide. So the input field should be 11*10px = 110px wide (I'm not taking the drawable icon into account here).
Here's what I've tried in XML:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_48"
app:startIconDrawable="@drawable/ic_phone">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="11"
android:inputType="phone"
android:maxLength="11"
android:maxLines="1"
android:minEms="11"
android:text="01"
android:textSize="20sp"
android:typeface="monospace" />
</com.google.android.material.textfield.TextInputLayout>
But this produces an output like the following:
As you can see, despite using ems
and minEms
, the field is 2 characters wide (it's 0 character wide if I don't add android:text
). But I want it to have a width of 11 characters (not more or less). Why is ems
not effective here and what's the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对我来说,给出的答案不起作用。但这做到了:
For me the given answers didn't work. But this did:
只需编辑您的布局宽度
从
到
我刚刚编辑了您的代码
Just edit your layout width
from
to
I just edited your code
您应该在
TextInputEditText
小部件中设置 11 位数字等宽文本(例如12345678901
),然后在片段中定义一个ViewTreeObserver.OnGlobalLayoutListener
,当布局可供测量时计算宽度。您可以在片段的
onResume()
中实例化观察者,并在onPause()
中将其删除。您还应该存储活动旋转期间的宽度。下面是一个完整的工作示例:
MainActivity.java:
activity_main.xml:
我在下面附上了结果的图像,其中我已经输入了 6 位数字:
You should set a 11 digits monospace text in your
TextInputEditText
widget (for example12345678901
), then define aViewTreeObserver.OnGlobalLayoutListener
in your fragment, that calculates the width when the layout is available to be measured.You could instantiate the observer in
onResume()
of your fragment, and remove it inonPause()
. You should also store the width during the rotation of the activity.Here below a full working example:
MainActivity.java:
activity_main.xml:
I enclose an image of the result here below, where I already typed 6 digits: