简体   繁体   English

EditText聚焦时隐藏键盘

[英]Keyboard hidden when EditText focused

I have two fragments in one Activity and both of the fragments contain one EditText. 我在一个Activity中有两个片段,两个片段都包含一个EditText。 When the the first EditText is focused (keyboard shown) and the user presses the next button of the keyboard, I am transferring the focus to the EditText in the second fragment by using this code: 当第一个EditText聚焦(显示键盘)并且用户按下键盘的下一个按钮时,我将使用以下代码将焦点转移到第二个片段中的EditText:

View next = autoCompleteTextView.focusSearch(View.FOCUS_DOWN);
    if (next != null) {
        next.requestFocus();
    }

The second EditText receives the focus as it should (the cursor starts blinking in it) but the keyboard that was shown, gets hidden. 第二个EditText按原样接收焦点(光标开始闪烁),但显示的键盘被隐藏。 I don't understand why this happens. 我不明白为什么会这样。 I tried million different solutions, to force the keyboard to be shown again but nothing works. 我尝试了百万种不同的解决方案,强制键盘再次显示,但没有任何效果。 I don't know why it gets hidden in the first place, I am just transferring focus. 我不知道为什么它首先被隐藏起来,我只是转移焦点。

The only thing that worked for me is this: 唯一对我有用的是:

 mComposeMsgBody.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus && mComposeMsgBody.isEnabled()) {

                mComposeMsgBody.post(new Runnable() {
                    @Override
                    public void run() {
                        final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(mComposeMsgBody, InputMethodManager.SHOW_IMPLICIT);
                    }
                });
            }
        }
    });

But it is not ideal, since the keyboard tries to get hidden, and then I am forcing it go up, so there is this 1 second down-up movement that the keyboard does. 但这不是理想的,因为键盘会试图隐藏起来,然后我强迫它向上移动,所以键盘会进行这1秒钟的向下移动。 If someone has a better solution for just transferring focus without the keyboard doing anything, please post an answer. 如果有人有一个更好的解决方案,只需转移焦点而不用键盘做任何事情,请发一个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM