Complete the FAQs on the soft keyboard in Android development

Source: Internet
Author: User

The principle of soft keyboard display

What is the nature of the software disk? The soft keyboard is actually a dialog.

Inputmethodservice created a dialog for our input method and set some parameters of the dialog window, such as gravity, to be displayed at the bottom or full screen. When we click on the input box, the system adjusts the active main window to make room for the input method, and then displays the dialog at the bottom or full screen.

Adjustment of Soft Keyboard display

Android defines a property named Windowsoftinputmode, which is used to set the interaction between the activity main window and the soft keyboard to avoid problems with the soft keyboard blocking the content. We can set the activity in Androidmanifet.xml. such as: Android:windowsoftinputmode= "Stateunchanged|adjustpan".

The optional value of this property has two parts, part of the soft keyboard state control, controls whether the soft keyboard is hidden or displayed, and the other part is the Activity window adjustment, in order to make room to show the soft keyboard.

The property setting of the Android:windowsoftinputmode must be one of the following values, or a "state" value plus a combination of "adjust" values, with each value | Separate.

stateunspecified-unspecified state: When we do not set the Android:windowsoftinputmode property, the software by default is this interaction mode, the system will be based on the interface to take the corresponding soft keyboard display mode.

stateunchanged-does not change state: The soft keyboard state of the current interface, depending on the soft keyboard state of the previous interface, whether it is hidden or displayed.

statehidden-Hidden state: When this state is set, the soft keyboard is always hidden, regardless of the input requirement.

statealwayshidden-always hides the state: When the state is set, the soft keyboard is always hidden, and the Statehidden is different, when we jump to the next interface, if the next page of the soft keyboard is displayed, and we come back again, the soft keyboard will be hidden.

statevisible-visible state: When set to this state, the soft keyboard is always visible, even if there is no input box on the interface can be forced to pop out.

Statealwaysvisible-always show the state: When set to this state, the soft keyboard is always visible, and statevisible is different, when we jump to the next interface, if the next page soft keyboard is hidden, and we come back again, the soft keyboard will be displayed.

adjustunspecified-no Pattern specified: Sets the display relationship between the soft keyboard and the software's display. This option is also the default setting mode when you are not setting this value with us. In this case, the system chooses different modes depending on the interface.

adjustresize-adjustment mode: In this mode, the window always adjusts the screen size to ensure that the soft keyboard display space; This option cannot be used with Adjustpan, and if neither of these properties is set, the system automatically selects one of them based on the layout in the window.

adjustpan-default mode: This mode will not be adjusted to ensure the space of the soft keyboard, but to take a different strategy, the system through the layout of the movement to ensure that the user to enter the input box must be in the user's field of view, so that users can see their input content.

Case

No scrolling layout xml

  

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

android:orientation= "Vertical" >

<edittext< p= "" >

Android:id= "@+id/et1"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 1"/>

<button< p= "" >

Android:id= "@+id/btn1"

Android:layout_width= "Match_parent"

android:layout_height= "Wrap_content"

android:layout_margintop= "5DP"

Android:text= "The monkey moved to the reinforcements button"

Android:textsize= "15SP"/>

<edittext< p= "" >

Android:id= "@+id/et2"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 2"/>

<edittext< p= "" >

Android:id= "@+id/et3"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 3"/>

<edittext< p= "" >

Android:id= "@+id/et4"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 4"/>

<edittext< p= "" >

Android:id= "@+id/et5"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 5"/>

<edittext< p= "" >

Android:id= "@+id/et6"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 6"/>

<edittext< p= "" >

Android:id= "@+id/et7"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 7"/>

<edittext< p= "" >

Android:id= "@+id/et8"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 8"/>

<edittext< p= "" >

Android:id= "@+id/et9"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 9"/>

<edittext< p= "" >

Android:id= "@+id/et10"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box"/>

<edittext< p= "" >

Android:id= "@+id/et11"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box one"/>

<edittext< p= "" >

Android:id= "@+id/et12"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box"/>

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485861234567891011121314151617181920212223242526272829303132333435363738394041424344 454647484950515253545556575859606162636465666768697071727374757677787980818283848586

For layouts that do not have a scrolling control, Adjustpan is the default setting, such as text input 8 in our case application, the text input box 123 and the button will be top up, and the page layout cannot be scrolled.

  

There is a scrolling layout xml

  

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent" >

<linearlayout< p= "" >

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

android:orientation= "Vertical" >

<edittext< p= "" >

Android:id= "@+id/et1"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 1"/>

<button< p= "" >

Android:id= "@+id/btn1"

Android:layout_width= "Match_parent"

android:layout_height= "Wrap_content"

android:layout_margintop= "5DP"

Android:text= "The monkey moved to the reinforcements button"

Android:textsize= "15SP"/>

<edittext< p= "" >

Android:id= "@+id/et2"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 2"/>

<edittext< p= "" >

Android:id= "@+id/et3"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 3"/>

<edittext< p= "" >

Android:id= "@+id/et4"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 4"/>

<edittext< p= "" >

Android:id= "@+id/et5"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 5"/>

<edittext< p= "" >

Android:id= "@+id/et6"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 6"/>

<edittext< p= "" >

Android:id= "@+id/et7"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 7"/>

<edittext< p= "" >

Android:id= "@+id/et8"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 8"/>

<edittext< p= "" >

Android:id= "@+id/et9"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box 9"/>

<edittext< p= "" >

Android:id= "@+id/et10"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box"/>

<edittext< p= "" >

Android:id= "@+id/et11"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box one"/>

<edittext< p= "" >

Android:id= "@+id/et12"

Android:layout_width= "Match_parent"

android:layout_height= "50DP"

android:text= "Text input box"/>

  

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091123456789101112131415161718192021222324252627282930313233343536373839 40414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091

For layouts with scrolling controls, the Adjustresize method is used, such as the text input 8 in our case application, the text input box 123 and the button above will be top up and you can scroll to see what is being topped up.

  

  

According to this principle, we can solve the problem of the soft keyboard occlusion page in the development, use ScrollView as the root layout, let the system adopt Adjustresize mode, it is a good solution to this problem.

Automatically eject the soft keyboard

Sometimes need to enter the activity after the automatic popup soft keyboard, you can set a time function to achieve, the specific wording is as follows:

Method One:

Timer timer=new timer ();

Timer.schedule (New TimerTask () {

public void Run () {

Inputmethodmanager inputmethodmanager= (Inputmethodmanager) Getsystemservice (Context.input_method_service);

Inputmethodmanager.togglesoftinput (0, inputmethodmanager.hide_not_always);

}

}, 1000); Automatically pops up after seconds 1234567812345678

Method Two:

Timer timer = new timer ();

Timer.schedule (New TimerTask () {

public void Run () {

Inputmethodmanager InputManager =

(Inputmethodmanager) Etinput.getcontext (). Getsystemservice (Context.input_method_service);

Inputmanager.showsoftinput (etinput, 0);

}

},

1000)///1 seconds after automatic popup 12345678910111234567891011

Do not eject the soft keyboard automatically

Sometimes after entering the activity does not want the system to automatically eject the soft keyboard, we can follow the following methods to achieve:

Method One:

Select the activity in the Androidmainfest.xml and set the Windowsoftinputmode property to Adjustunspecified|statehidden

  

Android:label= "@string/app_name"

Android:windowsoftinputmode= "Adjustunspecified|statehidden"

android:configchanges= "Orientation|keyboardhidden" >

  

  

  

  

123456789123456789

Method Two:

Let EditText lose focus, use EditText's Clearfocus method

EditText edit= (EditText) Findviewbyid (R.id.edit);

Edit.clearfocus (); 1212

Method Three:

Force Hide Android IME window

EditText edit= (EditText) Findviewbyid (R.id.edit);

Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service);

Imm.hidesoftinputfromwindow (Edit.getwindowtoken (), 0); 123123

Method Four:

EditText always does not eject the software keyboard

EditText edit= (EditText) Findviewbyid (R.id.edit);

Edit.setinputtype (inputtype.type_null); 1212

EditText setting ScrollView compress background picture solution

In your activity, add

GetWindow (). Setsoftinputmode (WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 11

Dynamic off soft keyboard

Sometimes you want to dynamically turn off the soft keyboard depending on the condition, and we can use the Inputmethodmanager class to do this in the following way:

Method One:

Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service); Get an example of Inputmethodmanager

if (imm.isactive ()) {//If Open

Imm.togglesoftinput (inputmethodmanager.show_implicit,inputmethodmanager.hide_not_always);//Turn off the soft keyboard, open the same method, This method toggles the open and closed state of the

}12341234

Method Two:

Force Hide Soft Keyboard

public void Keyboardcancle () {

View view = GetWindow (). Peekdecorview ();

if (view! = null) {

Inputmethodmanager Inputmanger = (inputmethodmanager) getsystemservice (Activitybase.input_method_service);

Inputmanger.hidesoftinputfromwindow (View.getwindowtoken (), 0);

}

}1234567812345678

Method Three:

int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;

GetWindow (). Addflags (flags); 1212

Method Four:

Under the OnClick event. The following methods are possible. (If the edittext loses focus/gets focus, no effect)

Inputmethodmanager im = (inputmethodmanager) getsystemservice (Context.input_method_service);

Im.hidesoftinputfromwindow (Getcurrentfocus (). Getapplicationwindowtoken (), inputmethodmanager.hide_not_always); 1212

Refer to the following links for specific usage of Inputmethodmanager:

Http://www.apihome.cn/api/android/InputMethodManager.html

Soft Keyboard Interface button function setting method

Use the Android:imeoptinos to make some interface settings on the Android-powered soft keyboard:

<edittext< p= "" >

Android:id= "@+id/text1"

Android:layout_width= "150dip"

android:layout_height= "Wrap_content"

android:imeoptions= "Flagnoextractui"/> 1234512345

android:imeoptions= "Flagnoextractui"//Make soft keyboard not full screen, only occupy part of the screen 11

This property also controls the display of keys in the lower right corner of the soft keyboard, which is the return key by default.

android:imeoptions= "Actionnone"//input box to the right without any hint

android:imeoptions= "Actiongo"//Bottom right button content is ' start '

android:imeoptions= "Actionsearch"//Bottom right button for magnifying glass image, search

android:imeoptions= "Actionsend"//lower right button content for ' send '

android:imeoptions= "Actionnext"//lower right button content for ' next '

android:imeoptions= "Actiondone"//Bottom right button content is ' done ' 123456123456

  

  

At the same time, it may be edittext to add the appropriate listener, capturing the user clicked on the Soft keyboard button in the lower right corner of the listener event for processing.

Edittext.setoneditoractionlistener (New Oneditoractionlistener () {

@Override

public boolean oneditoraction (TextView v, int ActionId, keyevent event) {

Toast.maketext (Mainactivity.this, "response to The configured keys", Toast.length_short). Show ();

return false;

}

}); 12345671234567

The hole that I stepped on

One, the soft keyboard can not top up the page

One of the requirements in development is to top up a button at the bottom of the page, but development finds that the Adjustresize property cannot be successfully Android5.0 after the version is set. Tangled for a long time, finally found in StackOverflow solution, that is, the root layout with fitssystemwindow= "true" can be.

  

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

Android:fitssystemwindows= "true"

>1234512345

  

Here the Fitssystemwindow specific role is whether your Contentview ignore Actionbar,title, the bottom of the screen virtual keys, the entire screen as a usable space.

Normally, the Contentview available space is the remaining usable area after the actionbar,title, the bottom key space is removed, and this property is set to True if ignored, false does not ignore

Second, the Custom Soft keyboard button function is invalid

Add android:imeoptions= "Actionsearch" to edittext this property is not responding, and finally found in version 2.3 and above does not work, the solution: Plus

Android:singleline= "true" 11

Because the Input method keyboard in the lower right corner of the default return line is used, when set up a single line, carriage returns will be lost, so that can be set to search, send, go and so on.

Complete the FAQs on the soft keyboard in Android development

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.