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

时间:2015-03-09 10:05:38

标签: android xml android-edittext

我对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中的3个。没有可能的新行,键盘显示“发送”而不是输入键,但文本继续在一行。

inputType="text"更改为"textMultiLine"会在多行上正确包装文本,但也会覆盖键盘以始终显示输入按钮。

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

我找不到有效的组合。

4 个答案:

答案 0 :(得分: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以防止粘贴功能。为了防止粘贴新行。

答案 1 :(得分:1)

输出:

enter image description here

Exp: 行动正轨。我进行了一些研究,发现一些以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);

答案 2 :(得分:0)

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

答案 3 :(得分:0)

很久以前我有同样的问题,您必须以编程方式更改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)