我的应用适用于
Android 2.2及更高版本.在其中,我使用
ActionbarSherlock来允许3.0之前的设备使用操作栏.我在操作栏中使用了EditText来允许用户输入文本进行搜索.
使用模拟器,使用Android 4.0和4.1(我没有尝试3.x,因为它不是平板电脑应用程序),当选择EditText时,软键盘会根据需要弹出.但不是这样使用Android 2.2或2.3.3,它只是不显示.
EditText的代码很简单:
item.setActionView(R.layout.collapsible_edittext); etInput = (EditText) item.getActionView().findViewById(R.id.etInput); etInput.requestFocus();
布局:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/etInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/search_hint"
android:lines="1"
android:maxLines="1"
android:inputType="textFilter|textNoSuggestions"
android:imeOptions="actionSend"/>
现在我尝试在etInput.requestFocus();之后立即使用此代码段专门显示软键盘,但它没有区别:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(etInput,InputMethodManager.SHOW_IMPLICIT);
我试图弄清楚这是ActionbarSherlock的问题还是更普遍的Android问题.我在一篇活动中搜索了许多关于强制软键盘显示的文章,但还没有找到解决方案.
谢谢
解决方法
我有同样的问题……一切正常,直到我在HTC Nexus One上测试.我最终将以下代码添加到附加到AciontBarSherlock menuItem的onActionExpandListener.
item.setonActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// post delayed to allow time for edittext to become visible
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mSearchText.clearFocus();
showKeyboard();
mSearchText.requestFocus();
}
},400);
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
hideKeyboard();
return true;
}
});
private void showKeyboard() {
if (android.os.Build.VERSION.SDK_INT < 11) {
mInputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
} else {
mInputManager.showSoftInput(mSearchText,InputMethodManager.SHOW_IMPLICIT);
}
}
private void hideKeyboard() {
if (android.os.Build.VERSION.SDK_INT < 11) {
mInputManager.hideSoftInputFromWindow(getActivity().getwindow().getCurrentFocus().getwindowToken(),0);
} else {
mInputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getwindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
}