Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...