Android EditText imeOptions 使actionSearch 的行为像actionDone?

Joz*_*eRi 1 keyboard android imeoptions

我有一个奇怪的错误。

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

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

searchField.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
Run Code Online (Sandbox Code Playgroud)

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

这是我的代码:

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;
                }
            });
Run Code Online (Sandbox Code Playgroud)

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

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

现在,我的键盘确实关闭了,但由于某种原因,它后面出现了一个“正常”键盘。带有搜索图标的键盘正在关闭,但出现带有输入图标的键盘,并且它没有输入任何内容..未连接到任何编辑文本或其他内容,我在该屏幕中只有 1 个编辑文本,我无法弄清楚怎么了。

如果我改回 IME_ACTION_DONE 一切都会再次正常。

编辑1:

我在清单中的活动:

<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>
Run Code Online (Sandbox Code Playgroud)

和我的 xml 中的 EditText :

<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" />
Run Code Online (Sandbox Code Playgroud)

知道我做错了什么吗?

另外,有没有办法可以在不使用 imeOption Search 的情况下显示搜索图标?

roy*_*oyB 5

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

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

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

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

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