[从片段中,我正在AlertDialog中使用SearchView,我希望键盘的“完成”按钮可以关闭键盘。由于某种原因,它不起作用。
这里是我的AlertDialog“
val mBuilder =
AlertDialog.Builder(context!!, Theme_DeviceDefault_Light_NoActionBar_Fullscreen)
val mView = View.inflate(activity!!, R.layout.list_search, null)
mBuilder.setView(mView)
val dialog = mBuilder.create()
val searchView = view.findViewById<SearchView>(R.id.search_view)
dialog.show()
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE)
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextChange(newText: String): Boolean {
adapterWithSearch.filter(newText)
return true
}
override fun onQueryTextSubmit(query: String): Boolean {
adapterWithSearch.filter(query)
return true
}
})
这是我的SearchView:
<SearchView
android:id="@+id/searchView"
style="@style/AppTheme.Searches"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:actionViewClass="android.support.v7.widget.SearchView"
android:iconifiedByDefault="false"
android:imeOptions="actionDone"
android:inputType="text"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true" />
我希望关闭键盘,而不是AlertDialog。任何人都有一个想法,为什么不解雇键盘?
我会尝试将其添加到您的代码中,当然您必须管理所需的字段
private View.OnKeyListener onSoftKeyboardDonePress=
new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
{
InputMethodManager inputManager = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, 0);
}
return false;
}
};
但是由于不清楚您的searchView是否为真正的SearchView,因此您也希望此reply
[我可能没有正确看到您要执行的操作,但看不到您要在哪里关闭键盘。对于此操作,您有两种选择:
尝试看一下,希望对您有所帮助
Close/hide the Android Soft Keyboard with Kotlin
您在这里有不同的解散键盘的方法。我认为这个问题没有更多的复杂性。