Android自动换行EditText文本框

85

我一直在尝试让我的EditText框自动换行,但似乎做不到。

在开发Android应用程序时,我遇到了更复杂的问题,而这个问题似乎应该是一个简单的过程。

然而,问题仍然存在,我有一个大文本框,只允许我在一行上输入文本,继续向右滚动,而我输入文本。

这是来自我的布局文件中EditText对象的XML代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/myWidget48"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
 <ScrollView
    android:id="@+id/myScrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    >
    <LinearLayout
        android:id="@+id/widget37"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
    >

<EditText
        android:id="@+id/txtNotes"
        android:layout_width="300px"
        android:layout_height="120px"
        android:scrollbars="vertical"
        android:textSize="18sp"
        android:gravity="left"
        android:layout_marginTop="10dip"
        android:inputType="textCapSentences"
        >
    </EditText>
</LinearLayout>
</ScrollView>
</LinearLayout>
12个回答

188
除了找到问题的来源外,我还找到了解决方案。如果使用了android:inputType属性,则必须使用textMultiLine属性来启用多行支持。此外,使用inputType会取代android:singleLine="false"属性。因此,需要重申一下,如果使用了inputType,则必须使用textMultiLine属性,否则EditText对象只包含一行且不自动换行。

编辑:感谢Jacob Malliet提供的更好建议。他建议将布尔值scrollHorizontally属性设置为false,即'android:scrollHorizontally="false"'

示例XML代码:

<EditText
    android:id ="@+id/edtInput"
    android:layout_width ="0dip" 
    android:layout_height ="wrap_content" 
    android:layout_weight ="1" 
    android:inputType="textCapSentences|textMultiLine"
    android:maxLines ="4" 
    android:maxLength ="2000" 
    android:hint ="@string/compose_hint"
    android:scrollHorizontally="false" />

1
我错过的是 android:layout_width = "0dp"。 - terencey
3
为什么要使用 android:layout_width ="0dip"?在我看来它是不可见的。 - Francisco Romero
@Error404 // 它将被 layout_weight="1" 属性覆盖。因此,它可能会放置在 LinearLayout 中。 - Youngjae
1
对于那些无法使此解决方案正常工作的人,只需将其包装在LinearLayout中(就像OP所包装的那样),它应该可以正常工作。 - jrhee17
所需的唯一内容是 textMultiLine,其余细节不必要。 - AaA

26

好的,我明白了,你必须在你的xml中为EditText设置android:scrollHorizontally="false"。我相信这应该会起作用。


2
五种不同的答案,只有这一个对我有效(除了那个可怕的表格,我甚至都没尝试)。 - yuttadhammo
1
这就是唯一的答案。不需要 LinesminLinesmaxLinesandroid:scrollHorizontally="false",只需设置 inputType 即可。 - Youngjae

21

假设你一开始只想要一行,然后将其扩展到5行,并且你希望最多有10行。

<EditText
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/etMessageBox"
   android:layout_alignParentLeft="true"
   android:layout_centerVertical="true"
   android:autoLink="all"
   android:hint="@string/message_edit_text_hint"
   android:lines="5"
   android:minLines="1"
   android:gravity="top|left"
   android:maxLines="10"
   android:scrollbars="none"
   android:inputType="textMultiLine|textCapSentences"/>

通过增加android:lines,你可以定义它会扩展到多少行。


19

你必须向你的EditText添加以下属性。

android:inputType="textMultiLine"

如果将EditText的高度设置为特定值或设置最大行数,当输入的字符超过限制时,它将会滚动。


11

我想到了解决方案。那行代码是:

android:inputType="textCapSentences"

问题所在。将此行添加到EditText对象中将使该对象成为单行EditText,并且不允许单词换行,或者允许用户按“Enter”来在EditText中插入换行符或回车符。


2
那么,只需使用android:inputType="textCapSentences|textMultiLine"即可。然后,您的EditText字段将以大写字母开头,而且它还可以是多行的。这对我很有效!现在我看到您已经在上面单独回答了... - snark

6

将整个活动放在滚动视图中,并将以下编辑文本放置在线性布局中,对我来说非常有效。

编辑文本会垂直滚动并且按Enter键,代码如下:

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="10"
    android:hint="Enter somehting"
    android:inputType="textMultiLine"
    android:maxLength="2000"
    android:maxLines="6"
    android:scrollHorizontally="false"
    android:scrollbars="vertical" > 

"输入一些东西" :D 但谁在乎 ;) #笔误 - fWd82

3

我试过这个方法,将重力改为“顶部|左侧”,以及使用多行文本输入类型,对我有用。

<EditText
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/color_red"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="top|left"
    android:inputType="textMultiLine|textCapSentences"
    android:lines="10"
    android:maxWidth="200dp"
    android:maxLength="200"
    android:maxLines="10"
    android:singleLine="false"
    android:textColor="@android:color/white"
    android:textSize="26sp" />

1
请使用 top | start 代替 top | left,以支持 RTL。 - Ohad Cohen

1

0

同时设置父布局宽度(Layout_width属性)。例如layout_width=250sp

否则,EditText或TextView不会在达到移动边界之前换行。因此,请定义Layout_width。


1
父容器指的是LinearLayout或TableLayout等布局。 - Victor

-1
试试这个。在我的编辑框里有效。
 android:gravity="top|start"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接