2010-07-18 43 views
55

我一直在試圖讓我的EditText框自動換行,但似乎無法做到。Android Word-Wrap 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> 

回答

133

除了找到問題的根源,我找到了解決辦法。如果使用android:inputType,則必須使用textMultiLine來啓用多行支持。另外,使用inputType將取代代碼android:singleLine="false"。如果使用inputType,則要重申,必須使用textMultiLine,否則EditText對象將只包含一行,而不包含換行符。

編輯:謝謝Jacob Malliet爲此提供了進一步的好建議。他建議將boolean scrollHorizo​​ntally屬性設置爲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」 – squeeish 2015-01-12 05:44:39

+3

爲什麼你使用'android:layout_width =「0dip」'?正如我所想的那樣,它對我來說是隱形的。 – 2015-08-22 14:31:11

+0

是爲multiautocompletetextview工作? – 2015-09-28 12:39:17

14

好吧,我想通了,你必須設置android:scrollHorizontally="false"爲您EditText在XML。我很確定這應該起作用。

+2

五個不同的答案,這是唯一一個爲我工作的(除了可怕的表,我甚至沒有嘗試過) – yuttadhammo 2012-07-20 13:06:18

+0

這是答案。不需要'Lines','minLines'。 'maxLines'和'android:scrollHorizo​​ntally =「false」'和'inputType'可以做到。 – Youngjae 2015-10-09 03:17:56

8

我想通了。該行,

android:inputType="textCapSentences" 

是問題。將此行添加到EditText對象將使該對象成爲單行EditText,並且不會讓單詞換行,或者允許用戶按下'Enter'在EditText中插入換行或回車。

+1

那麼,只需使用'android:inputType =「textCapSentences | textMultiLine」'來代替。然後你的EditText字段將以大寫_and_開頭,它將是多行的。這個對我有用!現在我看到你已經把它放在單獨的答案上面了... – snark 2016-06-15 17:58:30

-4
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/table" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1" 
    > 
    <TableRow 
     android:id="@+id/newRow"> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:orientation="vertical" 
      android:layout_height="wrap_content" 
      android:gravity="left" 
      android:paddingBottom="10dip"> 
      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:text="Some Text" 
      /> 
     </LinearLayout> 
     </TableRow> 

     <View 
     android:layout_height="2dip" 
     android:background="#FF909090" /> 

    <TableRow> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingTop="10dip"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"   
     android:text="Some Text" 
     android:paddingBottom="5dip" 
     />  
    <EditText 
     android:id="@+id/editbox" 
     android:layout_width="wrap_content" 
     android:layout_height="150px" 
     android:gravity="top" 
     android:inputType="textFilter" 
     android:scrollHorizontally="false"  
     /> 

</RelativeLayout> 
</TableRow> 
<TableRow> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <Button 
     android:id="@+id/btnone" 
     android:layout_width="3dip" 
     android:layout_height="wrap_content" 
     android:layout_margin="2dip" 
     android:layout_weight="1" 
     android:text="Btn" 
     />   
    <Button 
     android:id="@+id/btntwo" 
     android:layout_width="3dip" 
     android:layout_height="wrap_content" 
     android:layout_margin="2dip" 
     android:layout_weight="1" 
     android:text="Btn" 
     />  
</LinearLayout> 
</TableRow> 
</TableLayout> 
+3

請提供這個答案的一些細節 – 2015-07-01 22:43:11

13

假設你只是想有一條線在第一則擴大到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您可以定義擴展它多少行。

+0

這應該是接受的答案 – 2017-05-01 17:48:56

4

將滾動視圖中的整個活動括起來,並將以下編輯文本放入線性佈局中對我來說就像是魅​​力一樣。

如下

<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" > 
+0

''輸入somehting「':D但誰在乎;)#Typo – fWd82 2016-09-25 13:33:22

3

必須將以下屬性添加到您的EditText編輯文本將滾動本身垂直按下Enter鍵碼。

android:inputType="textMultiLine" 

此外,如果你設置的高度或EditText上的的maxlines設置爲特定值時,它會在你鍵入的字符太多滾動。