SoftKeyboard does not display for a newly displayed Fragment

回眸只為那壹抹淺笑 提交于 2019-11-29 09:25:57

On your onResume you can do this:

EditText someEditText = (EditText)getActivity().findViewById(R.id.someEditText);
someEditText.requestFocus(); 
InputMethodManager mgr =      (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(someEditText, InputMethodManager.SHOW_IMPLICIT);

I had a very similar issue. Fragment A calls fragment B where fragment B (Layout with TABS) has EditText in it. The keyboard would not come up for this EditText box unless I clicked on something else.

I tried the solution here, and on many other stackoverflow solutions. MANY. The only one that worked for me was to clear focus on the EditText when the EditText was clicked. In OnFocusChangeListener I was able to force the keyboard open and closed.

This issue only occurred for me on Android 2.34 devices and not 2.2, or 3.0. Emulator had no issues as well. Manifest only had adjustResize in it.

So here's a solution that worked for me(hopefully someone else finds this useful):

In onCreateView(...)

    //
    //Inflate Your Layout Here
    //

    //set on focus to force keyboard
    editText.setOnFocusChangeListener(new OnFocusChangeListener()
    {
        @Override
        public void onFocusChange(View v, boolean hasFocus) 
        {
            Log.d(TAG,"On Foucs. Has Focus = " + hasFocus);

            if (hasFocus)
            {
                 //open keyboard
                ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(v,
                        InputMethodManager.SHOW_FORCED);
            }
            else
            { 
                //close keyboard
                ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                        v.getWindowToken(), 0);
            }                               
        }
    });

    //Set on click listener to clear focus
    editText.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View clickedView) 
        {                           
            clickedView.clearFocus();
            clickedView.requestFocus();                             
        }       
    });    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!