force dialog input to require fullscreen ime soft

2019-07-22 19:19发布

I have a dialog box with an input, I have to automatically pop up the soft keyboard, current code:

    final EditText input = new EditText(this);
    final AlertDialog dialog = new AlertDialog.Builder(ScActivity.this)
            .setMessage(message)
            .setView(input).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
                    // do positive stuff
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
                }
            }).create();

    input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                        InputMethodManager.HIDE_IMPLICIT_ONLY);

            }
        }
    });
    dialog.show();
    input.requestFocus();

This works fine, except for one behavior. The first time I show this window in landscape the mode, the dialog box jumps up, as if it's trying to make room for the soft keyboard, then realizes there isn't enough room, jumps back down, then the fullscreen ime keyboard shows (with the text input).

It looks like a glitch. Don't want to live with it. Have tried doing things in different order, doing things on timers. Every consecutive time after the first, the keyboard shows up on top, no jumping. Anyone know of any workaround? I just want the soft keyboard to show up on top, fullscreen, in landscape mode (portrait mode has enough room for the dialog box to move up and be visible.

Thanks

0条回答
登录 后发表回答