强制对话框输入需要横向全屏 ime 软键盘

发布于 2024-10-20 03:21:58 字数 1759 浏览 1 评论 0原文

我有一个带有输入的对话框,我必须自动弹出软键盘,当前代码:

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

这工作正常,除了一种行为。我第一次以横向模式显示此窗口时,对话框会向上跳,好像它试图为软键盘腾出空间,然后意识到没有足够的空间,向下跳,然后显示全屏 ime 键盘(与文本输入)。

看起来像是一个小故障。不想忍受它。尝试过以不同的顺序做事,按计时器做事。第一次之后,每次连续,键盘都会显示在顶部,没有跳跃。有人知道任何解决方法吗?我只想在横向模式下全屏显示软键盘(纵向模式有足够的空间让对话框向上移动并可见。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文