1

I have an activity with a listview and each row has several editText. It looks like a questionnaire. I have for example the focus on one EditText, then I click on another EditText. At the moment, the keyboard disappear and reappear. It is very irritating.

I use that code to make the keyboard disppear when the user click anywhere else on the activity when he don't want to type. But if the user click on another editText, I would like the keyboard stay.

Someone has an idea if the keyboard is already shown, how to keep it open when I click on a EditText?

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    View w = getCurrentFocus();
    if (w instanceof EditText) {
        int scrcoords[] = new int[2];
        w.getLocationOnScreen(scrcoords);
        float x = event.getRawX() + w.getLeft() - scrcoords[0];
        float y = event.getRawY() + w.getTop() - scrcoords[1];

        if (event.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom()) ) {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
        }
    }
    return super.dispatchTouchEvent(event);
}
JavaJade
  • 199
  • 4
  • 13

2 Answers2

0

Make sure you're setting your android:inputType on your EditText declarations in XML. If this doesn't solve the issue by itself, you can specify next focuses using the View class's android:nextFocusUp, android:nextFocusDown, android:nextFocusRight, or android:nextFocusLeft to indicate the next EditText to move to.

Lastly, it could have something to do with how the ListView is handling focus. If that's the case, there's a related question here that would probably be worth looking at.

Community
  • 1
  • 1
Submersed
  • 8,810
  • 2
  • 30
  • 38
  • The inputType is set :`android:inputType="number"`. I don't think a can use `android:nextFocusDown` etc because I don't know whitch EditText the user will click after – JavaJade Feb 17 '15 at 16:50
0

Would be helpful if you could share your code.

I think what is happening is that your listview is causing your edit text to lose focus when you tap another one on a different row.

In general, what you could do is to register on a focus change listener on your edit text and then every time your edit text loses focus you can run a function to display the keyboard again.

replyInputEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            showKeyboard(editText);
        }
    });

The showkeyboard function :

public void showKeyboard(EditText field){

    field.setFocusable(true);
    field.setFocusableInTouchMode(true);
    field.requestFocus();
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(activity.getApplicationContext().INPUT_METHOD_SERVICE);
    imm.showSoftInput(field, InputMethodManager.SHOW_IMPLICIT);

}

You could also register an ontouchup listener on the text fields and then calling the showKeyboard function.

js123
  • 96
  • 2
  • 8