Android soft keyboard will not show in 2.2/2.3, but does in 3.0+

My app works on Android 2.2 and later. In it, I use ActionbarSherlock to allow the action bar usage for pre-3.0 devices. I used an EditText in the action bar to allow user text input for search.

Using the emulator, with Android 4.0 and 4.1 (I have not tried 3.x because it's not a tablet app), when the EditText is selected, the soft keyboard pops up as desired. But not so using Android 2.2 or 2.3.3, it just does not display.

The code for the EditText is straightforward:

item.setActionView(R.layout.collapsible_edittext);
etInput = (EditText) item.getActionView().findViewById(R.id.etInput);   
etInput.requestFocus();

Layout:

<?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"/>

Now I've tried specifically showing the soft keyboard using this snippet immediately after etInput.requestFocus(); , but it made no difference:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(etInput, InputMethodManager.SHOW_IMPLICIT);

I'm trying to figure out if this is an issue with ActionbarSherlock or a more general Android issue. I've scoured the many articles on forcing soft keyboard display in an Activity, but have not yet found the solution.

Thanks


I had the same problems... everything worked fine until i tested on HTC Nexus One. I ended up adding the following code to the onActionExpandListener attached to the AciontBarSherlock menuItem.

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);
 }  
}

对于那些在Mono机器人上使用C sharp的人,这是代码:

         new Thread(new ThreadStart(() =>
         {
             while (DateTime.Now < _dt)
                 Thread.Sleep(10);
             RunOnUiThread(showKeyboard);
         }
           )).Start();



     protected void showKeyboard()
     {
         int osSDK = (int)Android.OS.Build.VERSION.SdkInt;

         if (osSDK < 11)
         {
             this.mm().ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
         }
         else
         { 
            EditText editTextView = FindViewById<EditText>(Resource.Id.EditTextSubCellAddVenue);
            this.mm().ShowSoftInput(editTextView, ShowFlags.Implicit);
         }
     }

     protected void hideKeyboard()
     {
         int osSDK = (int)Android.OS.Build.VERSION.SdkInt;

         if (osSDK < 11)
         {
             this.mm().HideSoftInputFromWindow(this.Window.CurrentFocus.WindowToken, 0);
         }
         else
         {
             this.mm().HideSoftInputFromWindow(this.CurrentFocus.WindowToken, HideSoftInputFlags.NotAlways);
         }
     }

     protected InputMethodManager mm()
     {
         if (imm == null)
         {
             EditText editTextView = FindViewById<EditText>(Resource.Id.EditTextSubCellAddVenue);

             imm = (InputMethodManager)this.GetSystemService(Android.Content.Context.InputMethodService);
         }

         return imm;
     }
链接地址: http://www.djcxy.com/p/62352.html

上一篇: 防止使用ViewPager预加载下一个片段(制表符)

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