Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 按下向下箭头时隐藏键盘_Android - Fatal编程技术网

Android 按下向下箭头时隐藏键盘

Android 按下向下箭头时隐藏键盘,android,Android,图中显示了我的应用程序的一部分,一个带有连接适配器的AutoCompleteTextView。 当用户在该视图中输入内容时,将显示自动完成建议 我遇到的问题是:当显示建议并按下设备的向下箭头时,只有来自AutoCompleteTextView的建议关闭,键盘保持打开状态,需要再次点击向下箭头才能消失 我确实希望在第一次点击向下箭头时,建议和键盘消失 我尝试在backpressed上重写,但在点击向下箭头时没有调用它,可能是因为它没有被视为“back” 我怎么能这样做 编辑:我知道如何通过编程隐藏

图中显示了我的应用程序的一部分,一个带有连接适配器的
AutoCompleteTextView
。 当用户在该视图中输入内容时,将显示自动完成建议

我遇到的问题是:当显示建议并按下设备的向下箭头时,只有来自
AutoCompleteTextView
的建议关闭,键盘保持打开状态,需要再次点击向下箭头才能消失

我确实希望在第一次点击向下箭头时,建议和键盘消失

我尝试在backpressed上重写
,但在点击向下箭头时没有调用它,可能是因为它没有被视为“back”

我怎么能这样做


编辑:我知道如何通过编程隐藏键盘,我想我的问题是检测“向下箭头”点击

InputMethodManager inputManager = (InputMethodManager)
                                  getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                     InputMethodManager.HIDE_NOT_ALWAYS);


您需要导入
android.view.inputmethod.InputMethodManager

您可以尝试以下方法:

private boolean mIsKeyboardShown;
private EditText mSearchTextView;

@Override
protected void onCreate(Bundle bundle)
  ...
  mSearchTextView = (EditText) findViewById(R.id.search);
  View activityRootView = findViewById(R.id.activityRoot);
  activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
          int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
          // if more than 100 pixels, its probably a keyboard...
          mIsKeyboardShown = (heightDiff > 100);
       }
  });
}

public void onBackPressed() {
  if(mIsKeyboardShown) {
    // close the keyboard
    InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(mSearchTextView.getWindowToken(), 0);
  } else {
    super.onBackPressed();
  }
}

我没有尝试过该代码,但我认为这是正确的方法。

尝试在AutoCompleteTextView中覆盖
onKeyPreIme()
方法,如下所示:

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
        super.onKeyPreIme(keyCode, event);
        hideKeyboard()
        return true;
    }
    return super.onKeyPreIme(keyCode, event);
}

试试看这个[Close/hide the Android Soft Keyboard][1][1]:我知道如何通过编程隐藏键盘,我想我的问题是检测“向下箭头”点击。也许我很笨,但我不明白你指的是哪个向下键?来自物理键盘的那个?@Haspemulator他指的是屏幕截图底部的三角形。正常情况下,它指向左边,但现在向下。只需说出屏幕上的
后退按钮即可!!!更详细的移植: