简体   繁体   English

当用户键入时,如何防止 EditText 调整自身大小?

[英]How do I prevent an EditText from resizing itself when the user is typing?

I have an EditText and a Button set next to each other on the same horizontal line.我有一个EditText和一个Button设置在同一条水平线上彼此相邻。 It looks great, except when the user enters a lot of text, the EditText is resized, and the Button is squished.它看起来很棒,除了当用户输入大量文本时, EditText被调整大小,并且Button被压扁。

I have both EditText and Button set to layout_width="wrap_content" .我将EditTextButton设置为layout_width="wrap_content" "fill_parent" messes up the layout, and I don't want to use absolute sizes if I don't have to - the way it is now looks great in both landscape and portrait, I just don't want the EditText to resize. "fill_parent"弄乱了布局,如果没有必要,我不想使用绝对大小 - 现在它在横向和纵向看起来都很棒,我只是不希望EditText调整大小。

My layout:我的布局:

<TableLayout
    android:id="@+id/homelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow>
        <TextView
            android:id="@+id/labelartist"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Find artists:" />
    </TableRow>
    <TableRow>
        <EditText
            android:id="@+id/entryartist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:background="@android:drawable/editbox_background"
            android:editable="true"
            android:padding="5px"
            android:singleLine="true" />
        <Button
            android:id="@+id/okartist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10dip"
            android:layout_weight="1"
            android:text="Search" />
    </TableRow>

</TableLayout>

For EditText use对于 EditText 使用

android:layout_width="0dp" - not required but preferred.
android:layout_weight="1"

And for Button dont specify android:layout_weight对于 Button 不要指定android:layout_weight

What I do is in the onCreate for the activity, measure the EditText first then apply its maxWidth.我所做的是在活动的 onCreate 中,首先测量 EditText,然后应用其 maxWidth。

You can do so using code similar to the following:您可以使用类似于以下内容的代码执行此操作:

EditText someedittext = (EditText)findViewById(R.id.sometextview);
someedittext.setMaxWidth(someedittext.getWidth());

Give EditText a maxWidth .EditText一个maxWidth That should stop it from resizing beyond the width you provided.这应该会阻止它调整大小超出您提供的宽度。 Also, if you want it to be contained within 1 line set the maxLines setting to 1 too.此外,如果您希望它包含在 1 行内,请将maxLines设置也设置为 1。

在此处输入图片说明

You need to add android:imeOptions to prevent this behavior.您需要添加 android:imeOptions 来防止这种行为。

Just use:只需使用:

android:layout_width="fill_parent"

for both EditText and Button.对于 EditText 和 Button。 It will also work set for EditText but it is better to have it on both.它也适用于 EditText,但最好同时使用它。

you can define size of your edittext as it shows above您可以定义编辑文本的大小,如上所示

android:minWidth="80dp"
android:maxWidth="80dp"

or要么

android:layout_width="60dp"

or要么

you can apply a background image of size you want but don't forget to define its height and width with wrap_content.您可以应用所需大小的背景图像,但不要忘记使用 wrap_content 定义其高度和宽度。

  android:layout_width="wrap_content"
  android:layout_height="wrap_content"

I had a similar problem, but I couldn't get the above to work.我有一个类似的问题,但我无法使上述工作正常工作。 What I did, is take in the text from EditText box and then format it down to the max size that my screen could handle.我所做的是从 EditText 框中获取文本,然后将其格式化为我的屏幕可以处理的最大尺寸。 When I save this off to the DB, I will save it as display Text and original text.当我将其保存到数据库时,我会将其保存为显示文本和原始文本。

If you go to this page I put a longer explanation.如果你去这个页面,我会给出更长的解释。

I had the exact query.我有确切的查询。 But i had an EditText and a Button next to each other in a Linear Layout.但是我在线性布局中有一个 EditText 和一个 Button 彼此相邻。 I tried android:layout_weight="1" for EditText android:layout_width="0dp" And it worked just perfect!!我试过android:layout_weight="1" for EditText android:layout_width="0dp" 它工作得非常完美!! Thanks a lot!非常感谢!

The chosen solution works, but let me add a little complement:选择的解决方案有效,但让我补充一点:
if you use android:layout_weight="0.15" (the value is not important)如果您使用android:layout_weight="0.15" (该值不重要)
then然后
android:layout_width="0dp" if the LinearLayout is Horizontal android:layout_width="0dp"如果LinearLayout是 Horizo​​ntal
or要么
android:layout_height="0dp" if the LinearLayout is Vertical. android:layout_height="0dp"如果LinearLayout是 Vertical。

The correct way to do this would be to set the android:minWidth and android:maxWidth equal to the width you want.正确的方法是将 android:minWidth 和 android:maxWidth 设置为您想要的宽度。 This way you will not have to adjust your layout to use the android:layout_weight.这样您就不必调整布局来使用 android:layout_weight。

For example if you want your EditText to always be 80 density pixels no matter how many characters you type in use the following:例如,如果您希望 EditText 始终为 80 密度像素,无论您输入多少个字符,请使用以下内容:

android:minWidth="80dp"
android:maxWidth="80dp"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击时如何防止光标将其自身放置在EditText的开头 - How do I prevent cursor from placing itself at the beginning of EditText when clicked 如何防止用户在android studio中从键盘输入特定字符? - How do I prevent user from typing specific characters from the keyboard in android studio? 在 EditText 上键入时防止 android 保存和检索密码 - Prevent android from saving and retrieving passwords when typing on EditText 使用SpannableStringBuilder添加图像跨度后,如何防止光标在EditText(MultiAutoCompleteTextView)中调整大小? - How can I prevent the cursor from resizing in an EditText (MultiAutoCompleteTextView) after I add an imagespan using a SpannableStringBuilder? 用户完成输入后,如何选择 editText 中的所有文本? - How do I select all the text in an editText after the user is finished typing in it? 用户暂停在android edittext中键入内容时如何触发动作? - How to trigger an action when user has paused typing in android edittext? 如何从 Android Studio 的第一行开始在 EditText 中输入我的描述框? - How do I start typing my description box in EditText from the top line in Android Studio? 如何在Android的EditText字段中输入语音 - How do I have voice typing in my EditText field in Android 如何防止Android浮动EditText提示覆盖EditText中的文本? - How do I prevent Android floating EditText hint from covering the text in the EditText? 当用户输入 edittext 时,提示或文本值不会从 edittext 中删除 - Hint or Text value is not getting removed from edittext when user is typing in edittext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM