我试图获得SearchView的输入连接。我想做这个b/c我有一个应用内键盘,每当用户想搜索时,我都会显示我的应用内键盘。为此,我应该使用onCreateInputConnection方法来获取SearchView的输入连接,但是该方法对SearchView返回null,但对EditText运行良好。此外,我正在使用导航栏上的SearchView作为菜单项。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu_advanced_search, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
CustomAmharicKeyboard customAmharicKeyboard = findViewById(R.id.keyboard);
customAmharicKeyboard.setVisibility(View.VISIBLE);
InputConnection inputConnection = searchView.onCreateInputConnection(new EditorInfo());
customAmharicKeyboard.setInputConnection(inputConnection);
}
SearchView
并没有扩展EditText
——事实上,它是一个包含多个视图的布局。如果你想从SearchView
获得InputConnection
,你可以试试这个
InputConnection inputConnection = searchView.findViewById(R.id.search_src_text).onCreateInputConnection(new EditorInfo());