Java Android-requestFocus关闭键盘

Java Android-requestFocus关闭键盘,java,android,android-layout,Java,Android,Android Layout,当用户按键盘上的enter键时,我将焦点从一个AutoCompleteTextView切换到另一个。问题是,当下一个AutoCompleteTextView获得焦点时,键盘总是隐藏。有什么办法可以防止吗? 下面是我用来切换焦点的代码: field1.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCod

当用户按键盘上的enter键时,我将焦点从一个AutoCompleteTextView切换到另一个。问题是,当下一个AutoCompleteTextView获得焦点时,键盘总是隐藏。有什么办法可以防止吗? 下面是我用来切换焦点的代码:

field1.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_ENTER) {
                        field1.dismissDropDown();
                        field2.requestFocus();

                        return true;
                    }
                    return false;
                }
            });

我没有在声明字段1和字段2的XML文件中使用任何选项。

尝试在
requestfocus()之后以编程方式显示键盘。


是的,切换焦点会使键盘消失。一个快速修复方法是通过编程方式告诉键盘保持可见:

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

根据AutoCompleteTextView的源代码:

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        // Perform validation if the view is losing focus.
        if (!focused) {
            performValidation();
        }
        if (!focused && !mPopup.isDropDownAlwaysVisible()) {
            dismissDropDown();
        }
    }
我想你可以删除#field1.dismissDropDown()行-也许这会对你有所帮助

此外,我认为您应该设置一个DitorActionListener而不是OnKeyListener。我建议您尝试上面的代码:

  field1.setOnEditorActionListener(new AutoCompleteTextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      if (actionId == EditorInfo.IME_ACTION_ENTER) {
        field2.requestFocus();
        return true;
      }
      return false;
    }
  });
要强制打开:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
结束:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0);
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0);