如何允许在Android的EditText视图多行?


当前回答

 <EditText
           android:id="@id/editText" //id of editText
           android:gravity="start"   // Where to start Typing
           android:inputType="textMultiLine" // multiline
           android:imeOptions="actionDone" // Keyboard done button
           android:minLines="5" // Min Line of editText
           android:hint="@string/Enter Data" // Hint in editText
           android:layout_width="match_parent" //width editText
           android:layout_height="wrap_content" //height editText
           /> 

其他回答

另一件整洁的事情是使用android:minHeight和android:layout_height="wrap_content"。通过这种方式,您可以在编辑为空时设置编辑的大小,当用户输入内容时,它会根据用户输入的数量进行扩展,包括多行。

 <EditText
           android:id="@id/editText" //id of editText
           android:gravity="start"   // Where to start Typing
           android:inputType="textMultiLine" // multiline
           android:imeOptions="actionDone" // Keyboard done button
           android:minLines="5" // Min Line of editText
           android:hint="@string/Enter Data" // Hint in editText
           android:layout_width="match_parent" //width editText
           android:layout_height="wrap_content" //height editText
           /> 

EditText有singleLine属性。你可以在XML中设置或者调用setSingleLine(false); http://developer.android.com/reference/android/widget/TextView.html#setSingleLine%28%29

<EditText
                android:hint="Address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:inputType="textMultiLine|textNoSuggestions"
                android:id="@+id/edittxtAddress"
                android:layout_marginLeft="@dimen/textsize2"
                android:textColor="@android:color/white"
                android:scrollbars="vertical"
                android:minLines="2"
            android:textStyle="normal" />

并在活动中删除“\n”是用户按下nextline

String editStr= edittxtAddress.getText().toString().trim();

 MyString= editStr.replaceAll("\n"," ");

这是我从http://www.pcsalt.com/android/edittext-with-single-line-line-wrapping-and-done-action-in-android/上学到的,尽管我自己并不喜欢这个网站。如果你想要多行,但想要保留enter按钮作为发布按钮,将列表视图的“水平滚动”设置为false。

android:scrollHorizontally=“false”

如果它在xml中不起作用,那么以编程方式实现它就可以了。

listView.setHorizontallyScrolling(假);