Android软键盘不会在2.2/2.3中显示,但会在3.0+中显示

mra*_*tor 7 android android-softkeyboard android-edittext actionbarsherlock

我的应用适用于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();
Run Code Online (Sandbox Code Playgroud)

布局:

<?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"/>
Run Code Online (Sandbox Code Playgroud)

现在我尝试使用此片段后立即专门显示软键盘etInput.requestFocus();,但它没有任何区别:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etInput, InputMethodManager.SHOW_IMPLICIT);
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚这是ActionbarSherlock的问题还是更普遍的Android问题.我已经搜索了许多关于在Activity中强制软键盘显示的文章,但还没有找到解决方案.

谢谢

小智 4

我遇到了同样的问题...一切正常,直到我在 HTC Nexus One 上进行测试。我最终将以下代码添加到附加到 AciontBarSherlock 菜单项的 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);
 }  
}
Run Code Online (Sandbox Code Playgroud)