我在ActionBar中有一个searchView.我想在用户完成输入时关闭键盘.我在searchView上有以下queryTextListener
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
// Do something
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
showProgress();
// Do stuff, make async call
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return true;
}
};
Run Code Online (Sandbox Code Playgroud)
基于类似的问题,以下代码应该关闭键盘,但在这种情况下它不起作用:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
两者都不起作用.我不确定这是否是Honeycomb特定的问题,或者它是否与ActionBar中的searchView相关,或两者兼而有之.有没有人得到这个工作或知道为什么它不起作用?
我有一个searchView,看起来像这样:
private void setupSearchView() {
mSearchView = (SearchView) getActivity().findViewById(
R.id.search_view_neue);
setSearchViewBackground();
mSearchView.setOnClickListener(this);
mSearchView.setOnQueryTextListener(this);
}
public boolean onQueryTextSubmit(String query) {
searchcounter = searchcounter + 1;
setSearchViewBackground();
ArrayList<WissensdokumenteRecord> documents = settingListContent(new ArrayList<WissensdokumenteRecord>());
setListAdapter(new NeueWissensdokumentItemAdapter(
inflater.getContext(), R.layout.row_example, documents));
InputMethodManager im = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(getActivity().getCurrentFocus()
.getWindowToken(), 0);
return false;
}
<SearchView
android:id="@+id/search_view_neue"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:background="@drawable/border_searchview"
android:maxWidth="540dp"
android:queryHint="Neue Dokumente suchen" >
</SearchView>
Run Code Online (Sandbox Code Playgroud)
因此,搜索视图的行为是,通过单击搜索按钮打开键盘.现在我可以搜索一些东西了.是否可以通过点击搜索视图中的任意位置进行搜索?有没有人有我的榜样?
我有一个搜索视图,默认情况下设置为默认搜索查询,但我不想要虚拟键盘.在下面的代码中,我试图隐藏onCreateOptionsMenu中的键盘,但仍然可以看到键盘.
imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
MenuItem item = menu.findItem(R.id.menu_search);
item.expandActionView();
mSearchView = (SearchView) item.getActionView();
mSearchView.setIconifiedByDefault(false);
mSearchView.setQuery(query, true);
imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)
我正在使用sherlock搜索视图小部件.任何隐藏虚拟键盘的建议.我做错了什么?