Android EditText imeOptions 使 actionSearch 像 actionDone 一样?

Android EditText imeOptions making the actionSearch act like actionDone?

我有一个奇怪的错误。

我有一个 EditText,我正在使用 TextWatcher 执行搜索,当我键入 3 个及以上的字母时,我正在执行搜索。

直到最近我才有一个普通的 EditText,我想在我的键盘上有一个搜索图标所以我在代码中添加了我的 EditText:

searchField.setImeOptions(EditorInfo.IME_ACTION_SEARCH);

现在它有了搜索图标,现在是我的问题了。 当按下搜索图标时,我只想关闭键盘。无需搜索。

这是我的代码:

searchField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_SEARCH)) {
                        hideSoftKeyboard();
                    }
                    return false;
                }
            });

hideSoftKeyboard 是关闭键盘的方法,里面的代码是:

public void hideSoftKeyboard() {
    if (getActivity().getCurrentFocus() != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
    }
}

现在,我的键盘确实关闭了,但出于某种原因,"normal" 键盘出现在它后面。 带有搜索图标的键盘正在关闭,但是出现了带有输入图标的键盘,它没有输入任何内容..没有连接到任何编辑文本或其他东西,我在那个屏幕上只有 1 个编辑文本,我无法弄清楚怎么了。

如果我改回 IME_ACTION_DONE 一切又正常了。

编辑 1:

我的 activity 在清单中:

<activity
        android:name=".UI.activity.HomeActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Holo.Light.NoActionBar"
        android:windowSoftInputMode="adjustResize" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

我的 EditText 在 xml :

<EditText
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/search_document_main"
                    android:typeface="serif"
                    android:visibility="invisible"
                    android:singleLine="true"
                    android:background="@color/top_bar_bg_color"
                    android:hint="Search" />

知道我做错了什么吗?

此外,有没有一种方法可以在不使用 imeOption 搜索的情况下显示该搜索图标??

尝试从 EditText window 获取 window 令牌。也许您当前的焦点项目不是 EditText

public void hideSoftKeyboard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(
            Context.INPUT_METHOD_SERVICE
    );
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}

您还可以将监听器简化为:

new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction (TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                        hideKeyboard();
                        return true;
                    }
                    return false;
                }
            }

如果您仍有问题,请添加您的 Activity layout 并添加 Activity 清单声明

这适用于我对 actionSearch 的使用:

txtAutocomplete.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                boolean handled = false;
                if(actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED)
                {
                    txtAutocomplete.clearFocus();

                    InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(txtAutocomplete.getWindowToken(),0);

                    handled = true;
                }
                return handled;
            }
        });