Вопрос

Is there a way to get key events when forcing keyboard this way in Fragment onCreateView method:

    getActivity().getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

Also would need the keyboard to be in numeric mode.

Thanks.

Это было полезно?

Решение

You can listen keyboard presses if you override onKeyDown method in your activity

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            ...
            return true;
            ...
        default:
            return super.onKeyDown(keyCode, event);
        }
    }

Другие советы

There are two methods to show soft keyboard even if you dont have any edit text to focus on,

first-

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInputFromWindow(viewToAnchorTo.getWindowToken(), 0);

Second-

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top