Android XML - 没有换行符的 EditText 自动换行

标签 android xml android-edittext

我有一个关于 EditText 属性的愚蠢且看似微不足道的问题。

我试图为 EditText 实现的属性如下:

  • View 的内容不应包含换行符。可以是文本、数字、符号,但不能换行。

  • 由于上述原因,软键盘不应显示回车按钮。它应该显示类似“发送”或“完成”的内容。

  • View 的内容在到达屏幕边缘时不应水平继续。相反,我想将文本换行,以多行显示。

我尝试了很多不同的组合,但我无法实现这种组合。

我目前拥有的是这个,它在 RelativeLayout 中:

    <EditText
        android:id="@+id/comment_box"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_below="@id/preparation_text"
        android:hint="@string/comment_hint"
        android:inputType="textCapSentences|textAutoCorrect|text"
        android:scrollHorizontally="false"
        android:maxLength="400"
        android:imeOptions="actionSend"/>

它实现了 2 of 3。不可能换行,键盘显示“发送”而不是我的回车键,但文本在一行上继续。

inputType="text" 更改为 "textMultiLine" 可以在多行上正确地换行文本,但也会覆盖键盘以始终显示回车按钮。

我在 Internet 上尝试过不同的解决方案,包括设置属性 maxLines="4"singleLine="true" 以及可能的其他我又忘记了的解决方案。

我找不到有效的组合。

最佳答案

输出:

enter image description here

实验: 该行动走在正确的轨道上。我做了一些研究,发现 一些在 XML 中指定的选项被忽略。但如果在代码中设置了相同的选项,那么它应该可以解决问题。我使用了问题中指定的相同 XML fragment 。

 <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:hint="hint"
        android:inputType="textCapSentences|textAutoCorrect|text"
        android:scrollHorizontally="false"
        android:maxLength="400"
        android:imeOptions="actionSend"
       />

通过在代码中添加以下行,它有助于实现您想要的。

 edit_text.setMaxLines(Integer.MAX_VALUE);
 edit_text.setHorizontallyScrolling(false);

关于Android XML - 没有换行符的 EditText 自动换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28939330/

相关文章:

python - xml.etree.ElementTree.ParseError : not well-formed (invalid token) due to "<" symbol in script

java - EditText leftView 图片适合Android EditText的大小

android - 如何更改android中EditText提示的字体?

android - Unresolved reference 数据绑定(bind) - Android Studio 4.0

php - 上传 CSV 或 XML 进行编辑和返回

java - 在已经存在的 SharedPreference 值中更改数据类型

c# - 在 C# 中将列表转换为 xml

android - 值更改时 EditText setOnKeyListener 不起作用

android - Chromecast 在队列中转换视频一段时间后自动停止播放视频

android - 每个主要的 Android 广告提供商都需要哪些权限?