Android 中的 EditText 视图中允许多行吗?
如何在 Android 的 EditText
视图中允许多行?
How to allow multi-line in Android's EditText
view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 Android 的 EditText
视图中允许多行?
How to allow multi-line in Android's EditText
view?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(15)
默认情况下,Android 中的所有
EditText
小部件都是多行的。这是一些示例代码:
By default all the
EditText
widgets in Android are multi-lined.Here is some sample code:
您可能会发现它更好用:
这是因为
android:singleLine
已被弃用。You may find it better to use:
This is because
android:singleLine
is deprecated.这对我有用,实际上这两个属性很重要:inputType和lines。此外,您可能需要一个滚动条,下面的代码展示了如何制作一个:
This works for me, actually these 2 attributes are important: inputType and lines. Besides, you may need a scrollbar, the code below shows how to make one:
这就是我应用下面的代码片段的方式,并且运行良好。希望这会对某人有所帮助。
干杯! ...谢谢。
This is how I applied the code snippet below and it's working fine. Hope, this would help somebody.
Cheers! ...Thanks.
EditText 具有 singleLine 属性。您可以在 XML 中设置或通过调用 setSingleLine(false);
http://developer.android.com/reference/android /widget/TextView.html#setSingleLine%28%29
EditText has singleLine property. You can set in the XML or by calling setSingleLine(false);
http://developer.android.com/reference/android/widget/TextView.html#setSingleLine%28%29
试试这个,
将这些行添加到您的编辑文本视图中,我将添加我的。确保你理解它
,并在你的java类上点击listner来编辑文本,如下所示,我将根据你的添加我的chane名称。
这对我来说很好用
Try this,
add these lines to your edit text view, i'll add mine. make sure you understand it
and on your java class make on click listner to this edit text as follows, i'll add mine, chane names according to yours.
this works fine for me
如果有人使用 AppCompatEditText 那么你需要设置 inputType="textMultiLine" 这里是你可以复制
背景的代码
if some one is using AppCompatEditText then you need to set inputType="textMultiLine" here is the code you can copy
for background
该代码行对我有用。将这段代码添加到您的 xml 文件中的 edittext 中。
This code line work for me. Add this code you edittext in your xml file.
只需添加这个
android:inputType="textMultiLine"
在相关字段的 XML 文件中。
Just add this
android:inputType="textMultiLine"
in XML file on relevant field.
所有这些都很好,但如果您将编辑文本放在上层滚动视图中,则不起作用:) 也许最常见的示例是“设置”视图,该视图包含太多项目,以至于它们超出了可见区域。在这种情况下,您将它们全部放入滚动视图以使设置可滚动。如果您在设置中需要多行可滚动编辑文本,则其滚动将不起作用。
All of these are nice but will not work in case you have your edittext inside upper level scroll view :) Perhaps most common example is "Settings" view that has so many items that the they go beyond of visible area. In this case you put them all into scroll view to make settings scrollable. In case that you need multiline scrollable edit text in your settings, its scroll will not work.
要禁用先前在主题中分配的行数,请使用 xml 属性:
android:lines="@null"
To disable number of lines that was previously assigned in theme use xml attribute:
android:lines="@null"
我从 http://www.pcsalt.com/android/edittext-with-single-line-line-wrapping-and-done-action-in-android/,尽管我自己不喜欢这个网站。如果您想要多行但希望保留输入按钮作为发布按钮,请将列表视图的“水平滚动”设置为 false。
android:scrollHorizontally="false"
如果它在 xml 中不起作用,那么以编程方式执行它会很奇怪。
listView.setHorizontallyScrolling(false);
I learned this from http://www.pcsalt.com/android/edittext-with-single-line-line-wrapping-and-done-action-in-android/, though I don't like the website myself. If you want multiline BUT want to retain the enter button as a post button, set the listview's "horizontally scrolling" to false.
android:scrollHorizontally="false"
If it doesn't work in xml, doing it programmatically weirdly works.
listView.setHorizontallyScrolling(false);
另一个巧妙的做法是将
android:minHeight
与android:layout_height="wrap_content"
一起使用。这样,您可以在编辑为空时设置编辑的大小,并且当用户输入内容时,它会根据用户输入的数量(包括多行)进行拉伸。Another neat thing to do is to use
android:minHeight
along withandroid:layout_height="wrap_content"
. This way you can set the size of the edit when it's empty, and when the user inputs stuff, it stretches according to how much the user inputs, including multiple lines.在“活动”中删除“\n”是用户按下一行
and in Activity to remove "\n" is user press nextline