简体   繁体   中英

Android: Keyboard dismissal results in scrolling to the bottom

I'm using ViewPager to display two fragments as pages in my application. In page 1, there are EditText fields. I want to programmatically dismiss the keyboard associated with any of those textfields (in Page 1), when I scroll to Page2 (or select Tab2). I've written the following code for that:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {


        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            View currentView = getCurrentFocus();
            if (currentView != null) {

                InputMethodManager imm = (InputMethodManager)  currentView.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(currentView.getWindowToken(), 0);
            }

        } });

If the keyboard is visible while in Page 1, tapping on the second tab will result in Page 2 displayed in a 'scrolled down' state. This makes the Toolbar goes up, over the status bar making the Toolbar title almost invisible to read. Please check the screenshots below (1st image is Page1 and second is Page2):

第1页 第2页

How to rectify this?

try this it works

 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { switch ((actionId)) { case EditorInfo.IME_NULL:break; } return false; } }); 

and call editText.onEditorAction(EditorInfo.IME_NULL);

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