Android Word-Wrap EditText text

2019-01-08 12:04发布

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

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

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.

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

9条回答
乱世女痞
2楼-- · 2019-01-08 12:46

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" > 
查看更多
ら.Afraid
3楼-- · 2019-01-08 12:48

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.

查看更多
Emotional °昔
4楼-- · 2019-01-08 12:49

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.

<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:lines you can define expand it how many lines.

查看更多
登录 后发表回答