문제

In my app after some event i want my user to fill EditText. In order to do that, i request focus on this EditText and try to show keyboard using this code:

public void onGivenEvent(int which) {
  mMyDialog.dismiss(getActivity());
  mMyEditField.requestFocus();
  InputMethodManager imm =
      (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
  imm.showSoftInput(mMyEditField, InputMethodManager.SHOW_IMPLICIT);
}

xml for mMyEditField looks like this:

<EditText
  android:id="@+id/edit_field"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:layout_marginLeft="10dp"
  android:background="@null"
  android:digits="0123456789.,"
  android:freezesText="true"
  android:hint="some hint"
  android:imeOptions="actionDone"
  android:inputType="numberDecimal"
  android:maxLength="15"
  android:selectAllOnFocus="true"
  android:singleLine="true"
  android:textColor="@android:color/black"
  android:textSize="16sp" />

The funny thing is, this code doesn't show keyboard on first attempt. When this code is executed once, after device rotation keyboard is shown properly. I had similiar problem with android:selectAllOnFocus="true"not working correctly, but i used workaround - I set OnFocusChangeListener with mMyEditField.selectAll() inside.

Any ideas how to fix this issue?

도움이 되었습니까?

해결책

Call This method from your onGivenEvent() function

public void showSoftKeyboard() {
        try {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top