EditText.setInputType vs. setRawInputType
Asked Answered
C

3

15

What is the difference between EditText.setInputType and setRawInputType.

I have a field that should allow for all characters, but I have a mode button that switches between numeric and alpha keyboard.

So I want the numeric keyboard when they are "part number" search mode, but alpha keyboard when they are "description" search mode.

Android OS 2.2 or later.

Churchless answered 29/3, 2012 at 21:8 Comment(0)
D
11

setRawInputType() is usually used when you initialize the view, in a constructor of a custom view or in onCreate() method of an activity, etc. It's the same as if you set inputType with the XML attribute android:inputType. For example:

setContentView(R.layout.main);
mEditText = (EditText) findViewById(R.id.edit_text);
mEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
...

In your situation to change the mode of the soft keyboard that is shown for the editor on the fly you have to call setInputType() which also takes care of restarting soft keyboard.

setInputType(InputType.TYPE_CLASS_NUMBER) changes keyboard layout to numeric text setInputType(InputType.TYPE_CLASS_TEXT) changes keyboard layout to normal text

Dorchester answered 29/3, 2012 at 23:4 Comment(2)
Why setInputType(InputType.TYPE_CLASS_TEXT) changes the EditText?Pauly
I had a bug where I was using raw, and this solved it for me. ThanksAgitator
T
3

setInputType

Added in API level 3

public void setInputType (int type)

Set the type of the content with a constant as defined for EditorInfo#inputType. This will take care of changing the key listener, by calling setKeyListener(android.text.method.KeyListener), to match the given content type. If the given content type is EditorInfo#TYPE_NULL then a soft keyboard will not be displayed for this text view. Note that the maximum number of displayed lines (see setMaxLines(int)) will be modified if you change the EditorInfo#TYPE_TEXT_FLAG_MULTI_LINE flag of the input type.

setRawInputType

Added in API level 3 public void setRawInputType (int type) Directly change the content type integer of the text view, without modifying any other state.

Related XML Attributes:

android:inputType

Travistravus answered 26/10, 2020 at 6:36 Comment(0)
K
0

setRawInputType() does not format the entered text, and this is one of the benefits of using it.

Komsomolsk answered 13/12, 2022 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.