How do I hide the soft keyboard when changing tabs?

后端 未结 5 1343
耶瑟儿~
耶瑟儿~ 2021-01-05 13:06

EDIT : Seems I\'m not making myself clear. What I need is a way to hide the soft keyboard whenever i replace the fragment I am in. How do I go about doing this ?

相关标签:
5条回答
  • 2021-01-05 13:50

    programmatically you could use, capturing the view of the active activity on the screen of the device.

    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);
        }
    
    0 讨论(0)
  • 2021-01-05 13:50

    I had the same issue and placed the following code in my tab fragment just prior to using the FragmentTransaction.replace() method to change tabs. The onCreate and onCreateView methods in each fragment are not triggered after the initial tab selection, so hiding the keyboard can be done before getting to the specific fragment's class. Using mTabHost.getApplicationWindowToken() rather than editText.getWindowToken() was a major help. Thanks to whoever found that. Sorry I lost the link.

    InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);             
    im.hideSoftInputFromWindow(mTabHost.getApplicationWindowToken(), 0);
    
    .......
    fm = getFragmentManager();
    ....
    
    fm.beginTransaction()
        .replace(placeholder, new someFragment(), tabId)
        .commit();
    
    0 讨论(0)
  • 2021-01-05 14:04

    In your XML file just remove android:focusable="true" from everywhere.

    One more thing is if you are using <requestfocus></requestfocus> then also remove this line.

    Try it I think it should work.

    0 讨论(0)
  • 2021-01-05 14:09

    This is how you enable the soft keyboard

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

    And this is how you close it when you switch tabs.

    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    
    0 讨论(0)
  • 2021-01-05 14:09

    set OnPageChangeListener to viewpager in mainActivity Where you add fragments to 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);
    }
    
    0 讨论(0)
提交回复
热议问题