Android soft Keyboard Auto Eject and close

Source: Internet
Author: User

When we write modification information or search, change the password and other interface, the main purpose of the user to enter this interface is to input modify/find some information, in order to user experience should automatically eject the soft keyboard instead of letting the user actively click the input box to pop up.

1. Automatic popup of Soft keyboard
privatevoidshowKeyboard(){        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);        0);    }

When the interface is more complex, the soft keyboard may not eject properly and need to be loaded lazily. That is, after the interface loading is complete, the soft keyboard pops up

    • 1.1 Using the Timer schedule
new Timer();         timer.schedule(new TimerTask()   {             publicvoidrun() {                 InputMethodManager inputManager = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);                 0);             }         998);
    • 1.2 Using handler

2. Soft keyboard off

I have encountered a situation is that the home page is ScrollView wrapped interface, rolling a distance into the next page, will pop up the soft keyboard, when the interface is closed (that is, direct finish ()), back to the home page, ScrollView is not the original position. The solution is to have a soft keyboard pop-up page, first off the soft keyboard, then the finish () interface.

privatevoidcloseKeyboard() {        View view = getWindow().peekDecorView();        ifnull) {            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);            0);        }    }
3. The soft keyboard to squeeze some layout up the situation
<activity                android:name=".activity.DetailActivity"                android:screenOrientation="portrait"                android:windowSoftInputMode="adjustPan"                android:theme="@style/AppTheme.NoActionBar.Translucent"/>

This property is mainly Windowsoftinputmode, of which 2 of the more important are adjustpan and adjustresize
Adjustpan does not squeeze the bottom layout, such as the layout of the bottom in the relatelayout layout
The adjustresize is self-adapting and will squeeze the bottom up.

More detailed can understand the next windowsoftinputmode this attribute, a lot of great God's blog has a description, I do not repeat.

Android soft Keyboard Auto Eject and close

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.