且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android EditText多行无法正常工作

更新时间:2023-01-01 11:10:17

来自此

From this comment, the inputType was set in the code as well with:

textMessage.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE |
                         InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

这实际上是不正确的,因为TYPE_TEXT_FLAG_MULTI_LINETYPE_TEXT_FLAG_CAP_SENTENCES只是标志,并且不包含实际的输入类型.为了使它们起作用,必须将它们分层放置为InputType.TYPE_CLASS_TEXT上方的标志.如果没有此类型类标志,则编辑文本不具有可将您的标志应用到的基本输入类型类,并且默认为没有指定的输入类型.

This is actually not correct, because TYPE_TEXT_FLAG_MULTI_LINE and TYPE_TEXT_FLAG_CAP_SENTENCES are only flags, and do not contain the actual input type. In order for them to work, they must be layered as flags on top of InputType.TYPE_CLASS_TEXT. Without this type class flag, the edit text does not have a base input type class to apply your flags to, and defaults to no specified input type.

因此,使用这两个标志设置输入类型的正确方法是:

So, the correct way to set the input type with both of these flags is:

textMessage.setInputType(InputType.TYPE_CLASS_TEXT |
                         InputType.TYPE_TEXT_FLAG_MULTI_LINE |
                         InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

有关这些标志如何工作的官方详细信息,请参见 TextView-android:inputType

For official details on how these flags work, see the Android Developer Docs on InputType and TextView - android:inputType

我不确定为什么要这样设计.就我个人而言,我认为他们应该隐藏它们如何表示其标志(如ints/bit标志),而应该为它们的公共接口使用Enum枚举和/或InputType的子类.

I'm not sure why the design decision is this. Personally, I think they should have hidden how they are representing their flags (as ints/bit flags), and instead had enums and/or subclasses of InputType for their public interface.