【问题标题】:Android XML - EditText word-wrapping without newlinesAndroid XML - 没有换行符的 EditText 自动换行
【发布时间】:2015-05-10 10:49:51
【问题描述】:

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

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

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

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

  • 视图的内容在到达屏幕边缘时不应水平继续。相反,我想包装文本,将其显示在多行上。

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

我目前拥有的是这个,它在一个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" 以及我可能又忘记的其他属性。

我找不到有效的组合。

【问题讨论】:

  • 这对你有用吗?
  • 一半对一半。请查看对您的答案的评论。

标签: android xml android-edittext


【解决方案1】:

输出:

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

 <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);

【讨论】:

  • 成功了!非常感谢!
  • @MarkBuikema 很高兴为您提供帮助
【解决方案2】:

textShortMessage 是您正在寻找的inputType

<EditText
        android:id="@+id/commentEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="100dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
        android:gravity="left"
        android:text=""
        android:hint="hint"
        android:textSize="16dp"
        android:textColor="@android:color/black"
        android:lineSpacingExtra="0dp"
        android:lineSpacingMultiplier="1"
        android:background="@android:color/white"
        android:selectAllOnFocus="true"
        android:inputType="textShortMessage|textMultiLine|textCapSentences"
        android:singleLine="false" />

请参阅this 以防止粘贴功能。防止粘贴新行。

【讨论】:

  • 您对singleLineinputType 的选择确实改变了一些东西。文本现在可以正确换行,并且没有换行符的选项。然而,它似乎仍然覆盖了imeOptions 的属性。它现在显示笑脸而不是“完成”或“发送”。反正有这个问题吗?我认为这是使用textShortMessage 的属性。
  • 在我的应用程序中。我将这种方法与 Button 一起用于发送(键盘外)。
  • 这也是我的首选解决方案,但不是我想要的。
【解决方案3】:

请使用android:imeOptions="actionSend" 来解决你的问题。

【讨论】:

  • @Ashvinsolanki 这实际上确实提出了一个解决方案。我不知道它是正确的还是高质量的——但它一个答案。
【解决方案4】:

很久以前我有同样的问题,您必须以编程方式更改 OnCreate() 上的 Edittext 属性。

所以在 XML 中,像这样创建你的 Edittext

<EditText
       android:id="@+id/comment_box"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:minHeight="80dp"
       android:hint="Comment here"
       android:maxLength="400"
       android:inputType="textMultiLine" />

在 onCreate() 上(我用 kotlin 而非 Java 编写)

comment_box.imeOptions = EditorInfo.IME_ACTION_SEND
comment_box.setRawInputType(InputType.TYPE_CLASS_TEXT)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多