您好我使用了如下的searchview,
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.all_menu, menu);
SearchManager manager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(manager.getSearchableInfo(getActivity().getComponentName()));
searchTextView = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchTextView, R.drawable.cursor); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
} catch (Exception e) {
e.printStackTrace();
}
if (searchView != null) {
searchView.setQueryHint(getString(R.string.search));
searchView.setOnQueryTextListener(this);
}
mMenu = menu;
setNotificationCount(mMenu);
}
并且我使用以下代码使通知计数无效,
getSupportActionBar().invalidateOptionsMenu();
但是菜单无效将关闭打开的键盘。即使在操作栏菜单无效后我也想保留键盘状态。所以我试图使用下面的代码保留键盘状态,
KeyboardVisibilityEvent.setEventListener(getActivity(), new KeyboardVisibilityEventListener() {
@Override
public void onVisibilityChanged(boolean isOpen) {
if(!isFristTime){
Log.d("ss","keyboard status before validated "+isKeyboardOpened);
isKeyboardOpened = isOpen;
isFristTime = true;
}
}
});
getSupportActionBar().invalidateOptionsMenu();
Log.d("ss","keyboard status after validated "+isKeyboardOpened);
if(isKeyboardOpened){
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
Log.d("ss","keyboard status in try ");
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchTextView, R.drawable.cursor); //This sets the cursor resource ID to 0 or @null which will make it visible on white background
BDevice.showSoftKeyboard(getActivity(), searchTextView);
isKeyboardOpened = false;
getSupportActionBar().invalidateOptionsMenu();
} catch (Exception e) {
Log.d("ss","keyboard status in catch ");
e.printStackTrace();
}
}
}, 1000);
}
但这不起作用。现在我想打开软键盘,如果它打开,然后使菜单无效。你可以建议我在菜单无效后保留键盘状态吗?
答案 0 :(得分:0)
我找到了解决方案。每当验证操作栏菜单onCreateOptionsMenu获得调用。因此,我在第一次打开键盘时存储键盘状态,并在验证后检查onCreateOptionsMenu中的键盘状态。然后我用来扩展搜索视图并以编程方式打开软键盘,如下所示,
全局变量
private boolean isFristTime = false, isKeyboardOpened = false;
private String mCurrentQuery = "";
然后我的代码在onCreateOptionsMenu方法
if(isKeyboardOpened) {
searchView.setIconified(false);
searchItem.expandActionView();
searchView.requestFocus();
if(!TextUtils.isEmpty(mCurrentQuery)){
searchView.setQuery(mCurrentQuery, false);
}else{
searchView.setQueryHint(getString(R.string.search));
}
searchView.setIconifiedByDefault(true);
BDevice.showSoftKeyboard(getActivity(), searchTextView);
isKeyboardOpened = false;
}
我正在验证之前启动键盘,如下所示,
KeyboardVisibilityEvent.setEventListener(getActivity(), new KeyboardVisibilityEventListener() {
@Override
public void onVisibilityChanged(boolean isOpen) {
if(!isFristTime){
isKeyboardOpened = isOpen;
isFristTime = true;
}
}
});
getSupportActionBar().invalidateOptionsMenu();
并在onQueryTextChange方法中获取我的查询
@Override
public boolean onQueryTextChange(String newText) {
mCurrentQuery = newText;
return true;
}
快乐编码......