【问题标题】:Android Word-Wrap EditText textAndroid Word-Wrap EditText 文本
【发布时间】:2011-03-17 14:59:06
【问题描述】:

我一直在尝试让我的 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>

【问题讨论】:

    标签: android xml android-edittext word word-wrap


    【解决方案1】:

    除了找到问题的根源之外,我还找到了解决方案。如果使用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" />
    

    【讨论】:

    • 我错过的是 android:layout_width = "0dp"
    • 你为什么使用android:layout_width ="0dip"?正如我所料,它对我来说是不可见的。
    • @Error404 // 它将被layout_weight="1" 属性覆盖。所以它可能放在LinearLayout
    • 对于那些无法使用此解决方案的人,只需将其包裹在一个线性布局中(就像包裹它的 OP 一样),它应该可以正常工作。
    • 唯一需要的就是textMultiLine,其余的细节都不需要
    【解决方案2】:

    好的,我想通了,您必须在 xml 中为您的 EditText 设置 android:scrollHorizontally="false"。我很确定这应该可行。

    【讨论】:

    • 五个不同的答案,这是唯一对我有用的答案(除了我什至没有尝试过的可怕的桌子)
    • 这就是答案。不需要LinesminLinesmaxLinesandroid:scrollHorizontally="false"inputType 可以做到。
    【解决方案3】:

    假设您一开始只想有一行,然后将其扩展为 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,您可以定义扩展它的行数。

    【讨论】:

      【解决方案4】:

      您必须将以下属性添加到您的编辑文本中。

      android:inputType="textMultiLine"
      

      此外,如果您设置高度或将编辑文本的最大行数设置为特定值,当您输入很多字符时它会滚动。

      【讨论】:

        【解决方案5】:

        我想通了。线,

        android:inputType="textCapSentences"
        

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

        【讨论】:

        • 那么,请改用android:inputType="textCapSentences|textMultiLine"。然后您的 EditText 字段将以大写 开头,它将是多行的。这个对我有用!现在我看到你已经把它放在上面的单独答案中......
        【解决方案6】:

        将整个活动封闭在滚动视图中,并以线性布局放置以下编辑文本,这对我来说就像一个魅力。

        按回车,编辑文本会垂直滚动,代码如下

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

        【讨论】:

        • "Enter somehting" :D 但谁在乎 ;) #Typo
        【解决方案7】:

        试试这个,将重力更改为 top|left 对我有用,以及输入类型 textmultiline

        <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" />
        

        【讨论】:

        • 请使用 top|start 而不是 top|left,以获得 rtl 支持。
        【解决方案8】:

        对于那些以编程方式执行此操作的人,您可能需要查看此问题Android EditText Multiline does not work as it should 以仔细检查您是否正确设置了输入类型。那是我的问题

        【讨论】:

          【解决方案9】:

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

          否则,EditText 或 TextView 在到达移动边框的末尾之前不会换行到下一行。所以定义 Layout_width

          【讨论】:

          • 父方式,LinerLayout 或 Tablelayout 等
          【解决方案10】:

          注意: 第 1 步 - 为 EditText wordWrap 创建一个自定义类。

          Android 没有这个属性。但是你可以用 ReplacementTransformationMethod 替换所有中断字符。

          public class WordBreakTransformationMethod extends ReplacementTransformationMethod
          {
              private static WordBreakTransformationMethod instance;
              private WordBreakTransformationMethod() {}
          
              public static WordBreakTransformationMethod getInstance()
              {
                  if (instance == null)
                  {
                      instance = new WordBreakTransformationMethod();
                  }
                  return instance;
              }
          
              private static char[] dash = new char[] {'-', '\u2011'};
              private static char[] space = new char[] {' ', '\u00A0'};
          
              private static char[] original = new char[] {dash[0], space[0]};
              private static char[] replacement = new char[] {dash[1], space[1]};
          
              @Override
              protected char[] getOriginal()
              {
                  return original;
              }
          
              @Override
              protected char[] getReplacement()
              {
                  return replacement;
              }
          }
          

          第 2 步 - 在 Activity 中,编写以下代码,

          在安卓中:

          myEditText.setTransformationMethod(WordBreakTransformationMethod.getInstance());
          

          在 Kotlin 中:

          myEditText.setTransformationMethod= WordBreakTransformationMethod.getInstance
          

          在 XML 中:

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal">
              <EditText
                  android:id="@+id/myEditText"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:inputType="textCapSentences|textMultiLine"
                  android:scrollHorizontally="false" />
          </LinearLayout>
          

          【讨论】:

            【解决方案11】:
            <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>
            

            【讨论】:

            • 请提供这个答案的一些细节
            猜你喜欢
            • 2011-07-07
            • 1970-01-01
            • 2012-05-31
            • 1970-01-01
            • 2016-05-17
            • 1970-01-01
            • 1970-01-01
            • 2022-01-22
            • 2011-05-15
            相关资源
            最近更新 更多