prevent回车键上的EditText,但仍显示文本的多线回车键、但仍、文本、prevent

2023-09-12 11:14:56 作者:回忆爱情里的折磨

我如何在Android上一个EditText,使得用户不能输入多行文本,但显示效果还是多行(即有自动换行,而不是文字去到正确的)?

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.

推荐答案

我会继承窗口小部件并覆盖关键事件处理,以阻止输入键:

I would subclass the widget and override the key event handling in order to block the Enter key:

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