简体   繁体   中英

Android soft keyboard and Enter action

I have a EditText in my app which is supposed to take only numeric values; this is why I set the inputType property this way:

android:inputType="number|numberSigned|numberDecimal"

It works properly, except when the user presses enter, because it losts focus; is there any property to set to avoid this? Thank you for your answers.

What do you want to do? Keep the focus?

You can implement this method and check the value... if the value is not what you expect, you put back the focus.

public void onFocusChange(View v, boolean hasFocus) {
  // ...
}

Regarding your comment:

public void onFocusChange(View v, boolean hasFocus) {
  if (!hasFocus && (v instanceof EditText)) {
    ((EditText) v).requestFocus();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM