How do I set multiple input types in an EditText on Android?

北慕城南 提交于 2020-03-17 08:35:05

问题


I am trying to create an EditText with auto-capitalization and auto-correction implemented. I have manually figured out how to add InputFilters 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);

Please note, I am only interested in solutions for creating this EditText in code - not via XML.

Edit

I found sound new documentation describing TextKeyListener, however after trying to use this:

mEditText.setKeyListener(new TextKeyListener(TextKeyListener.Capitalize.CHARACTERS, true));

and using @farble1670's idea of using setRawInputType, so as not to affect the KeyListeners, there is still no change to the text.


回答1:


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.




回答2:


I hope you've found an answer to the question. The answer might help those those come to the thread later. So, you can set multiple tags in similar manner as you do in XML using a | (pipe). Something like:

EditText mEditText = new EditText(this);
mEditText.setInputType(InputTpe.TYPE_TEXT_FLAG_CAP_CHARACTERS|InputType.TYPE_TEXT_FLAG_AUTO_CORRECT);

Also, depending on your situation you might want to use setInputTypeor setRawInputype.




回答3:


yes, it seems like that should work. however, looking at the docs,

The type of data being placed in a text field, used to help an input method decide how to let the user enter text. The constants here correspond to those defined by InputType. Generally you can select a single value, though some can be combined together as indicated. Setting this attribute to anything besides none also implies that the text is editable.

http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType

so it looks like in general, you can't expect to set two values. The above link shows which flags can be combined together.

also, if you look at android:setInputType, it says this maps to the setRawInputType() method, not setInputType(). you might try calling setRawInputType() in stead of setInputType().

http://developer.android.com/reference/android/widget/TextView.html#setRawInputType(int)



来源:https://stackoverflow.com/questions/9771550/how-do-i-set-multiple-input-types-in-an-edittext-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!