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


当前回答

只要加上这个 android: inputType = " textMultiLine " 在XML文件的相关字段。

其他回答

你可能会发现更好的用法是:

<EditText 
...
android:inputType="textMultiLine"
/>

这是因为android:singleLine已弃用。

android:inputType="textMultiLine"

这行代码对我有用。在xml文件中添加您编辑的代码。

<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"," ");
 <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
           /> 

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

android:scrollHorizontally=“false”

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

listView.setHorizontallyScrolling(假);