【已解决】 SearchView+ListView关闭自动弹出的输入法
本人是刚入门的菜鸡。就是我学习了SearchView+ListView实现了一个模糊匹配,然后发现这个输入法在你没有点击SearchView时就已经自动弹出来了,在网上查阅了相关资料关闭输入法无效,想实现的效果是一进入SearchView+ListView这个界面时不会一开始就自动弹出输入法或者是当我没有点击SearchView时输入法不会弹出来,输入法弹出来以后点击SearchView以外的地方输入法自动关闭。下面是我的代码以及xml。
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private final String[] mStrings = Cheeses.sCheeseStrings;
private SearchView mSearchView;
private ListView mListView;
private ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
mSearchView = (SearchView) findViewById(R.id.search_view);
mListView = findViewById(R.id.list_view);
mListView.setAdapter(mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,mStrings));
mListView.setTextFilterEnabled(true);
setupSearchView();
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String str = (String) ((TextView)view).getText();
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
}
});
}
private void setupSearchView() {
//设置搜索框直接展开显示。左侧有放大镜(在搜索框中) 右侧有叉叉 可以关闭搜索框
//mSearchView.setIconified(false);
//设置搜索框直接展开显示。左侧有放大镜(在搜索框外) 右侧无叉叉 有输入内容后有叉叉 不能关闭搜索框
//mSearchView.setIconifiedByDefault(false);
//设置搜索框直接展开显示。左侧有无放大镜(在搜索框中) 右侧无叉叉 有输入内容后有叉叉 不能关闭搜索框
mSearchView.onActionViewExpanded();
//为 SearchView 中的用户操作设置侦听器。
mSearchView.setOnQueryTextListener(this);
//当查询非空时启用显示提交按钮。
mSearchView.setSubmitButtonEnabled(false);
//查询提示语句
mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint));
}
//用户输入字符时激发该方法
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
// mListView.clearTextFilter();
// mAdapter.notifyDataSetChanged();
mListView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1));
} else {
// mListView.setFilterText(newText.toString());
List<String> list = new ArrayList<String>();
int length = mStrings.length;
for(int i = 0 ; i < length ; i++){
if(mStrings[i].contains(newText)){
list.add(mStrings[i]);
}
}
String[] newString = list.toArray(new String[list.size()]);
mListView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,newString));
}
return true;
}
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
super.onPointerCaptureChanged(hasCapture);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
这个问题可以这样搞,进入页面之后,把焦点设置给一个view,比如你的页面中一个父布局,又或者一个tv都行,进入页面获取到控件之后,执行下面的代码。
让一个普通的组件主动拿到焦点了,搜索的组件就不会请求焦点,弹出键盘了。