Prevent enter key on EditText but still show the t

2019-01-16 13:29发布

How do I make an EditText on Android such that the user may not enter a multi-line text, but the display is still multi-line (i.e. there is word-wrap instead of the text going over to the right)?

It's similar to the built-in SMS application where we can't input newline but the text is displayed in multiple lines.

18条回答
爷的心禁止访问
2楼-- · 2019-01-16 14:25

For a URI you could use:

android:inputType="textUri"
android:lines="1"
android:maxLength="128"

Otherwise android:inputType="textPersonName" as mentioned above works for other EditText such user name, etc.

查看更多
唯我独甜
3楼-- · 2019-01-16 14:25

Here is the solution....

<EditText
   android:id="@+id/editText"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:maxLength="150"                                 
   android:textSize="15dp"
   android:imeOptions="actionDone"
   android:inputType="text|textMultiLine"/>

Usage in java class

editText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int keyCode, KeyEvent event) {

            if (keyCode==KeyEvent.KEYCODE_ENTER)
            {
                // Just ignore the [Enter] key
                return true;
            }
            // Handle all other keys in the default way
            return (keyCode == KeyEvent.KEYCODE_ENTER);
        }
    });
查看更多
▲ chillily
4楼-- · 2019-01-16 14:26

Simply add

        android:singleLine="true"

to your EditText

查看更多
冷血范
5楼-- · 2019-01-16 14:27

Try this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_ENTER)
    {
        //Nothing
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
查看更多
Ridiculous、
6楼-- · 2019-01-16 14:28

This one works for me:

<EditText
    android:inputType="textShortMessage|textMultiLine"
    android:minLines="3"
    ... />

It shows a smiley instead of the Enter key.

查看更多
别忘想泡老子
7楼-- · 2019-01-16 14:28

Here's a more correct answer that does not display the enter key on the IME keyboard:

// IMPORTANT, do this before any of the code following it
myEditText.setSingleLine(true);

// IMPORTANT, to allow wrapping
myEditText.setHorizontallyScrolling(false);
// IMPORTANT, or else your edit text would wrap but not expand to multiple lines
myEditText.setMaxLines(6);

Also, you may replace setSingleLine(true) with either, an explicit android:inputType on the XML layout file, or setInputType(InputType.*) on code – in which, the input type used, is anything that you know restricts the input to single lines only (i.e., anything that calls setSingleLine(true) implicitly already).


Explanation:

What setSingleLine(true) does is calling setHorizontallyScrolling(true) and setLines(1) implicitly, alongside with altering some IME keyboard settings to disable the enter key.

In turn, the call to setLines(1) is like calling setMinLines(1) and setMaxLines(1) in one call.

Some input types (i.e., the constants from InputType.TYPE_*) calls setSingleLine(true) implicitly, or at least achieves the same effect.

Conclusion:

So to achieve what the OP wants, we simply counter those implicit settings by reverting those implicit calls.

查看更多
登录 后发表回答