Only some Android devices with hardware keyboards and most of them have soft keyboards only. IMF handle all input works.
Usually, if there is no hardware keyboard, an input method editor (IME) will be availabled to users when they click on an
EditText
. If the default function of percussion is what you want to offer, you do not need to make any changes to your application code. Fortunately, Android is pretty clever to guess what you want, so you may need to experiment with IME and specific without changing code.However, percussion IME can not totally fit for your application. For example, It may covered many of
EditText
line. It would be nice if we can control the behavior of IME. Fortunately, the IMF as an overall tool gives you many options for this, will be described in this post.Choose input type for EditText
EditText
has a attribute called android:inputType
, by this, we can set the the suitable input type for our aim. Look at this example:
With the EditText
hasn't been set input type (no special rule), a default soft keyboard without special characters will be shown when it's focused:If input type value is
textEmailAddress
, we will have a @ character in soft keyboard interface, the DONE key is displayed with "Send icon" when we set android:imeOptions="actionSend"
:Input type value is
textPassword
, all input texts will be hidden:We can allowed user input only number to
EditText
by set input type is number
, numberSigned
or numberDecimal
:And the
date
input type is have "/" character in bottom left corner:Moreover, you can enlarge the
EditText
(not is only 1 line) interface by set input type to textMultiline
and use attributes minLines
, maxLines
:Force Hiding Soft Keyboard
EdiText
is focused. Sometimes, we would like to hide it by touching at screen (outside the EditText
). This work can be done by overriding dispatchTouchEvent()
method to detect touching action from user (if we touch outside, softkeyboard will hide and the EditText
will be unfocused):
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
View v = getCurrentFocus();
if (v != null &&
(ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) &&
v instanceof EditText &&
!v.getClass().getName().startsWith("android.webkit.")) {
int scrcoords[] = new int[2];
v.getLocationOnScreen(scrcoords);
float x = ev.getRawX() + v.getLeft() - scrcoords[0];
float y = ev.getRawY() + v.getTop() - scrcoords[1];
if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom())
hideKeyboard(this);
}
return super.dispatchTouchEvent(ev);
}
public static void hideKeyboard(Activity activity) {
if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) {
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
}
}
Conclusions
EditText
. More details, you can go to read the official doc page about InputMethodManager
or visit this tag link to read all posts about EditText
and input.