【问题标题】:keyboard doesn't hide on back arrow click on Autocomplete(GooglePlacesPicker)键盘不会隐藏在后退箭头上单击自动完成(GooglePlacesPicker)
【发布时间】:2020-02-12 06:34:33
【问题描述】:

List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.LAT_LNG, Place.Field.ADDRESS, Place.Field.NAME);
    // Start the autocomplete intent.
    Intent intent = null;

    if (initialQuery != null && !initialQuery.isEmpty()) {
        intent = new Autocomplete.IntentBuilder(
                AutocompleteActivityMode.FULLSCREEN, fields).
                setInitialQuery(initialQuery).
                setTypeFilter(TypeFilter.CITIES).
                build(activity);
    } else {
        intent = new Autocomplete.IntentBuilder(
                AutocompleteActivityMode.FULLSCREEN, fields).
                setTypeFilter(TypeFilter.CITIES).
                build(activity);
    }

    activity.startActivityForResult(intent, GOOGLE_PLACES_PICKER_REQUEST_CODE);

以上代码打开GooglePlacesPicker,我们可以在其中搜索地点。点击后退箭头应关闭键盘。但事实并非如此。

我尝试了以下方法来关闭键盘

   public static void hide(Context context) {
    try {
        if (context != null) {
            InputMethodManager imm = (InputMethodManager) context
                    .getSystemService(Activity.INPUT_METHOD_SERVICE);
            if (((Activity) context).getCurrentFocus() != null) {
                View currentFocus = ((Activity) context).getCurrentFocus();
                if (currentFocus.getWindowToken() != null) {
                    imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.SHOW_FORCED);
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

但没有奏效。请帮忙

【问题讨论】:

  • 你把隐藏软键盘的代码块放在哪里了? onActivityResult?
  • @GianhTran 是的
  • 错了,onActivityResult 在用户点击返回按钮时不会被调用
  • @GianhTran 我也在onResume() 上尝试过,但没有成功。你有什么建议?

标签: android google-places-api android-softkeyboard googleplacesautocomplete google-place-picker


【解决方案1】:

像这样设置关闭键盘按钮

hideButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {

                        InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        in.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), 0);
                    }
    });

【讨论】:

  • 使用软键盘助手类,如 SoftKeyboardHelper.hide(this, searchView);
  • 你能详细说明一下SoftKeyboardHelper吗?
  • 在 hideKeyboard() 方法中试试这个:getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
猜你喜欢
  • 1970-01-01
  • 2021-03-23
  • 2014-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多