问题
The SearchView is focused by default, but when I try to show software keyboard - it doesn't happen:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
But when I click on the SearchView - it does. Why?
回答1:
Fixed!
mSearchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
showInputMethod(view.findFocus());
}
}
});
And
private void showInputMethod(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(view, 0);
}
}
回答2:
I encountered this problem with a SearchView that still would not open the keyboard, and was able to do so by adding a 200ms delay:
mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(final View view, boolean hasFocus) {
if (hasFocus) {
view.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view.findFocus(), 0);
}
}, 200);
}
}
});
回答3:
If you are using SearchView:
<android.support.v7.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:iconifiedByDefault="true"
app:showAsAction="always"
app:iconifiedByDefault="true"/>
Then just add it to your onCreate method :
final SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setFocusable(true);
searchView.setIconified(false);
searchView.requestFocusFromTouch();
It works for me.
回答4:
What about using setOnFocusChangeListener
?
searchview.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
}
});
回答5:
Best and simplest way to do it :
android:focusable="true"
app:iconifiedByDefault="false"
android:focusableInTouchMode="true"
Add above three lines into your xml under SearchView and see the magic....!!
回答6:
Try:
void focusSearchViewAndOpenOrCloseKeyboard(boolean focus) {
searchView.setIconified(focus);
}
The source shows us that SearchView
handles its own closing and opening of the soft keyboard.
回答7:
I solved this issue in next simple way.
SearchView menu item:
<item
android:id="@+id/menu_item_search"
android:icon="@drawable/ic_search"
android:title="@android:string/search_go"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" />
To expand SearchView and show keyboard programmatically call:
mMenuItemSearch.expandActionView();
mSearchView.setQuery("your default query test or null", true);
Tested with different showAsAction flags. As result keyboard does not shown when flag: "always". However everything works with flag: "always|collapseActionView"
expandActionView() + setQuery("null or restored text", true); can be called when view created (or restored) to show keyboard and focus on search. P.S: call mSearchView.clearFocus(); after this two method to clear the focus.
来源:https://stackoverflow.com/questions/12022715/unable-to-show-keyboard-automatically-in-the-searchview