Domanda

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.

È stato utile?

Soluzione

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);
        }
    }

Altri suggerimenti

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top