如何在Android的EditText中设置多种输入类型? [英] How do I set multiple input types in an EditText on Android?
问题描述
我正在尝试创建具有自动大写和自动更正功能的EditText
.我已经手工弄清楚了如何添加InputFilter
来允许自动大写,尽管这只能在输入第一个字母后 起作用,而且我对自动更正没有运气(我尝试创建一个使用AutoText
的InputFilter,但我不确定所有工作原理如何).理想情况下,我可以只使用EditText.setInputType(...)处理所有内容,但是到目前为止,这还行不通.有没有办法做到这一点?我的尝试失败如下所示(我只能得到正常的输入).
I am trying to create an EditText
with auto-capitalization and auto-correction implemented. I have manually figured out how to add InputFilter
s to allow auto-capitalization, though this only works after the first letter is typed, and I have had no luck with auto correction (I tried to create an InputFilter that used AutoText
, but I'm not sure how all that works). Ideally, I could just use EditText.setInputType(...) to handle everything, but so far this has not worked. Is there a way to achieve this? My failed attempt is shown below (I just get normal input).
EditText mEditText = new EditText(this);
int inputType = InputType.TYPE_CLASS_TEXT;
if (auto_capitalize) {
inputType = mEditText.getInputType() | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
}
if (auto_correct) {
inputType = mEditText.getInputType() | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT;
}
mEditText.setInputType(inputType);
请注意,我只对通过代码创建此EditText
的解决方案感兴趣,而不是通过XML.
Please note, I am only interested in solutions for creating this EditText
in code - not via XML.
修改
我找到了完善的新文档,其中描述了 TextKeyListener ,但是在尝试使用后这个:
I found sound new documentation describing TextKeyListener, however after trying to use this:
mEditText.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.CHARACTERS, true));
并使用@ farble1670的使用setRawInputType
的想法,以免影响KeyListener
,文本仍然没有变化.
and using @farble1670's idea of using setRawInputType
, so as not to affect the KeyListener
s, there is still no change to the text.
推荐答案
通过XML可以像这样进行设置.
Through XML it would be setup like so.
android:inputType="textMultiLine|textNoSuggestions"
您只需在变量之间添加管道(|
).我看到您正在通过代码完成此操作,但我只是将其扔在那里以供参考.
You simply add a pipe (|
) between variables. I see you were doing it through code but I was just throwing this out there for reference.
这篇关于如何在Android的EditText中设置多种输入类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!