Android Word-Wrap EditText 文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3276380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:25:26  来源:igfitidea点击:

Android Word-Wrap EditText text

androidxmlandroid-edittextwordword-wrap

提问by Bryan

I have been trying to get my EditText box to word wrap, but can't seem to do it.

我一直试图让我的 EditText 框自动换行,但似乎无法做到。

I have dealt with much more complicated issues while developing Android applications, and this seems like it should be a straightforward process.

我在开发 Android 应用程序时处理过更复杂的问题,这看起来应该是一个简单的过程。

However, the issue remains, and I have a large text box that is only allowing me to enter text on one line, continuing straight across, scrolling horizontally as I enter text.

但是,问题仍然存在,我有一个大文本框,它只允许我在一行上输入文本,继续笔直​​地跨过,在我输入文本时水平滚动。

Here is the XML code for the EditText object from my layout file.

这是我的布局文件中 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>

回答by Bryan

Besides finding the source of the issue, I found the solution. If android:inputTypeis used, then textMultiLine must be used to enable multi-line support. Also, using inputType supersedes the code android:singleLine="false". If using inputType, then, to reiterate, textMultiLine must be used or the EditText object will only consist of one line, without word-wrapping.

除了找到问题的根源,我还找到了解决方案。如果android:inputType使用,则必须使用 textMultiLine 来启用多行支持。此外,使用 inputType 取代了 code android:singleLine="false"。如果使用 inputType,那么重申一下,必须使用 textMultiLine 否则 EditText 对象将只包含一行,而没有自动换行。

Edit:Thank you Jacob Mallietfor providing further good advice on this. He suggested to set the boolean scrollHorizontally property to false, 'android:scrollHorizontally="false"'.

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

Example XML code:

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

回答by Jacob Malliet

Ok i figured it out, you must set android:scrollHorizontally="false"for your EditTextin your xml. I'm pretty sure this should work.

好吧,我想通了,你必须设置android:scrollHorizontally="false"为您EditText在您的XML。我很确定这应该有效。

回答by Hesam

Assume that you just want to have one line at first then expand it to 5 lines and you want to have maximum 10 lines.

假设您一开始只想有一行,然后将其扩展为 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"/>

By increasing android:linesyou can define expand it how many lines.

通过增加android:lines您可以定义扩展它多少行。

回答by ?zer ?zcan

you must add the following attribute to your edittext.

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

android:inputType="textMultiLine"

Also if you set the height or set the maxlines of edittext to a specific value, it will scroll when you typed much character.

此外,如果您设置高度或将 edittext 的 maxlines 设置为特定值,则在您输入大量字符时它会滚动。

回答by Bryan

I figured it out. The line,

我想到了。线,

android:inputType="textCapSentences"

was the issue. Adding this line to an EditText object will make the object be a single line EditText, and will not let words wrap, or allow a user to press 'Enter' to insert a line feed or carriage return in the EditText.

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

回答by San

Enclosing the entire activity in scroll view and placing the following edit text with in linear layout worked like a charm for me.

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

The edit text would scroll itself vertically pressing enter, code as follows

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

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

回答by Jerry Sha

For those doing it programmatically, you might want to look at this question Android EditText Multiline does not work as it should to double check you are setting the input type correctly. That was my issue

对于那些以编程方式进行操作的人,您可能需要查看这个问题Android EditText Multiline 不起作用,因为它应该 仔细检查您是否正确设置了输入类型。那是我的问题

回答by Eeshdaya Umarikar

Try this, changing gravity to top|left worked for me, along with input type textmultiline

试试这个,将重力改为左上|左对我有用,输入类型 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" />

回答by Victor

Also set the parent layout width, (Layout_width property). For example layout_width=250sp

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

otherwise , EditText or TextView dont wrap to next line till it reach the end of mobile border. So define the Layout_width

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

回答by Shraddha Patel

Note: step 1 - create a custom class for EditTextwordWrap.

注意:第 1 步 - 为EditTextwordWrap创建自定义类。

Android hasn't this property. But you can replace all breaking characters with ReplacementTransformationMethod.

安卓没有这个属性。但是您可以使用 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;
    }
}

Step 2 - In Activity , write below code,

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

In Android :

在安卓中:

myEditText.setTransformationMethod(WordBreakTransformationMethod.getInstance());

In Kotlin:

在科特林:

myEditText.setTransformationMethod= WordBreakTransformationMethod.getInstance

In Xml:

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