How to hide soft keyboard when changing tabs?

EDITOR: Looks like I don't understand. I need a way to hide the soft keyboard whenever I replace the fragment I am in. How should I do it?

Let me keep it simple. I have an EditText window in the Fragment 1.2 tab, which obviously opens on the Soft Keyboard when pressed. How to hide this when tab is resized? I tried the following in my onTabSelected (), which seems to do nothing

getWindow().setSoftInputMode(
                  WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

I tried everything now. None of the proposed solutions that I have found so far in any way helps me.

+5
source share
5 answers

programmatically you can use, capturing the view of the active action on the device screen.

public final void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
        View focus = getCurrentFocus();
        if (focus != null) {
            hiddenKeyboard(focus);
        }
    }
public final void onTabselected(Tab tab, FragmentTransaction fragmentTransaction) {
        View focus = getCurrentFocus();
        if (focus != null) {
            hiddenKeyboard(focus);
        }
    }
public final void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
        View focus = getCurrentFocus();
        if (focus != null) {
            hiddenKeyboard(focus);
        }
    }

private void hiddenKeyboard(View v) {
        InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        keyboard.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
+11

, FragmentTransaction.replace() . onCreate onCreateView , . mTabHost.getApplicationWindowToken(), editText.getWindowToken() . , . , .

InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);             
im.hideSoftInputFromWindow(mTabHost.getApplicationWindowToken(), 0);

.......
fm = getFragmentManager();
....

fm.beginTransaction()
    .replace(placeholder, new someFragment(), tabId)
    .commit();
+6

inputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

.

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
+2

OnPageChangeListener viewpager mainActivity, viewPager

  viewPager.setOnPageChangeListener(myOnPageChangeListener);




   ViewPager.OnPageChangeListener myOnPageChangeListener =
            new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int state) {
                //Called when the scroll state changes.

            }

            @Override
            public void onPageScrolled(int position,
                                       float positionOffset, int positionOffsetPixels) {
                //This method will be invoked when the current page is scrolled,
                //either as part of a programmatically initiated smooth scroll
                //or a user initiated touch scroll.

                hideKeyboard();
            }

            @Override
            public void onPageSelected(int position) {
                //This method will be invoked when a new page becomes selected.
                //hide keyboard when any fragment of this class has been detached
                hideKeyboard();
            }
        };







public  void hideKeyboard() {
    InputMethodManager inputManager = (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);

    // check if no view has focus:
    View v = getCurrentFocus();
    if (v == null)
        return;

    inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
+2

XML android:focusable="true" .

, <requestfocus></requestfocus>, .

Try it, I think it should work.

0
source

All Articles