阻止在EditText上输入键,但仍将文本显示为多行

Ran*_*ku' 65 android multiline android-edittext

如何在Android上创建一个EditText,使得用户可能不会输入多行文本,但显示仍然是多行的(即有文字换行而不是文本转到右侧)?

它类似于内置的SMS应用程序,我们无法输入换行符,但文本以多行显示.

Ton*_*ony 46

我将子窗口小部件并覆盖键事件处理以阻止Enter键:

class MyTextView extends EditText
{
    ...
    @Override
    public boolean onKeyDown(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 super.onKeyDown(keyCode, event);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答.(我也提到了另一个答案).提示:您可以通过editText.setOnKeyListener(...)和return(keyCode == KeyEvent.KEYCODE_ENTER)来避免子类化. (10认同)
  • 应该是这样的:`return super.onKeyDown(keyCode,event);`在onKeyDown还是我在想象?:P (4认同)
  • 几乎没有键盘实际发送关键事件.这些是用于硬件密钥.他们几乎都使用commitText,它不会调用此函数. (3认同)
  • 输入键仍然在线性布局的nexus中工作 (2认同)

And*_*lph 42

这是一种方法,您不必重写EditText类.您只需捕获并用空字符串替换换行符.

myEditTextObject.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    public void afterTextChanged(Editable s) {

        for(int i = s.length(); i > 0; i--) {

            if(s.subSequence(i-1, i).toString().equals("\n"))
                s.replace(i-1, i, "");
        }

        String myTextString = s.toString();
    }
});
Run Code Online (Sandbox Code Playgroud)

  • +1这在我的情况下比接受的答案更有用,原因很简单,我可以将它用作TextWatcher的默认实现,可以通过子类扩展(这将简单地调用super.afterTextChanged(... )保留检查换行符.工作! (3认同)
  • 我认为这会抛出一个IndexOutOfBoundsException,用于复制和粘贴多个新行字符的新行. (2认同)

ser*_*y.n 24


XML中的属性

android:lines="5"
android:inputType="textPersonName"
Run Code Online (Sandbox Code Playgroud)

  • android:inputType ="textPersonName"不起作用 (3认同)

sog*_*ger 14

这个对我有用:

<EditText
    android:inputType="textShortMessage|textMultiLine"
    android:minLines="3"
    ... />
Run Code Online (Sandbox Code Playgroud)

它显示一个笑脸而不是Enter键.

  • 最佳答案,无需更改太多代码! (2认同)
  • 根据经验,这不适用于每个键盘.我们周围的HTC手机仍显示输入键. (2认同)

Jas*_*arc 9

这是一个更正确的答案,不会在 IME 键盘上显示回车键:

// 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);
Run Code Online (Sandbox Code Playgroud)

此外,您可以setSingleLine(true)使用android:inputTypeXML 布局文件或setInputType(InputType.*)代码中的显式替换,其中使用的输入类型是您知道将输入限制为单行的任何内容(即,任何setSingleLine(true)已经隐式调用的内容)。


解释:

什么setSingleLine(true)是调用setHorizontallyScrolling(true)setLines(1)隐式,同时更改一些 IME 键盘设置以禁用回车键。

反过来,调用 tosetLines(1)就像调用setMinLines(1)setMaxLines(1)在一个调用中。

某些输入类型(即来自 的常量InputType.TYPE_*setSingleLine(true)隐式调用,或至少达到相同的效果。

结论:

因此,为了实现 OP 想要的,我们只需通过恢复那些隐式调用来对抗那些隐式设置。


Rol*_*f ツ 7

@Andreas Rudolph提供的答案包含一个严重的错误,不应该使用.IndexOutOfBoundsException当您在EditText其中包含多个换行符的文本时,代码会导致该错误.这是由使用的循环类型引起的,Editable对象将在afterTextChanged其内容更改(替换,删除,插入)后立即调用该方法.

正确的代码:

edittext.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    public void afterTextChanged(Editable s) {
        /*
         * The loop is in reverse for a purpose,
         * each replace or delete call on the Editable will cause
         * the afterTextChanged method to be called again.
         * Hence the return statement after the first removal.
         * http://developer.android.com/reference/android/text/TextWatcher.html#afterTextChanged(android.text.Editable)
         */
        for(int i = s.length()-1; i >= 0; i--){
            if(s.charAt(i) == '\n'){
                s.delete(i, i + 1);
                return;
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)


Nir*_*tel 5

试试这个:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_ENTER)
    {
        //Nothing
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
Run Code Online (Sandbox Code Playgroud)

  • 不会.这会将编辑限制为单行.OP想要多条线; 他只是不希望用户能够输入换行符. (2认同)

小智 5

我正在测试它,它似乎工作:

EditText editText = new EditText(context);
editText.setSingleLine(false);
editText.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT);
Run Code Online (Sandbox Code Playgroud)