简体   繁体   中英

SoftKeyboard does not display for a newly displayed Fragment

I have a FragmentActivity that initially displays a fragment with a few buttons on it. When you click one of the buttons, the FragmentActivity displays a new fragment with some editText fields. I can't seem to get the soft input keyboard to display when my new fragment with the editText fields is displayed.

Using the windowSoftInput mode on the manifest is out as that displays the keyboard right away.

 android:windowSoftInputMode="stateUnchanged"

I have tried using

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)

to no avail. Here's how I display the new fragment from my Activity:

public void clickHandler(View view) {
        switch (view.getId()) {
        case R.id.login:
            loginFragment = new LoginFragment();
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();

            transaction.replace(R.id.fragment_container, loginFragment);
            transaction.addToBackStack(null); 
            transaction.commit();
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);  
            break;

...

I have also tried calling setSoftInputMode from within the fragment's onCreate and that has not worked as well. Thinking it was a timing issue I tried it with handler.postDelayed and that didn't work either. It looked like this:

onResume...    
Handler handler = new Handler();
            Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);                
                }
            };

            handler.postDelayed(runnable, 1000);

Any help will be appreciated. Thanks.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM