Android soft keyboard control method and some problems encountered in development.

Android soft keyboard control method and some problems encountered in development.

Android provides the windowSoftInputMode property to control the interaction between the input method soft keyboard window and the Activity main window, which is divided into the window size adjustment series and the input method soft keyboard display control series.

Window resizing series:

This series of parameters is used to control the adjustment strategy of the Activity main window when the soft keyboard pops up. If the main window is not adjusted, the current input control may be blocked by the soft keyboard.

adjustPan:

The main window of the Activity is not resized to make room for the input method, but the content of the window automatically shakes up and down to ensure that the currently focused control is not blocked by the keyboard, and the user can see what he or she is typing. Compared with the adjustResize mode, it is not very satisfactory because the user must close the input method to interact with the control blocked by the input method.

adjustResize:

The activity's main window will be resized to make room for the input method.

adjustUnspecified:

The current mode does not explicitly specify whether the Activity uses adjustPan or adjustResize. The system automatically selects a mode. If the current Activity's Window has a control that can scroll its own content, such as ScrollView, then adjustResize is selected because it believes that scrolling can make the content in the Window visible even in a small area. This is the default mode for Activity.

adjustNoting:

There is no change in the Activity's Window.

In both modes, the relationship between screen – main window – main window content – ​​soft keyboard is as follows:

The specific effects are as follows:

Without scrolling content, adjustPan:

Without scrolling content, adjustResize:

There is no scrolling content, adjustUnspecified == adjustPan

There is scrolling content (ScrollView), adjustPan

There is scrolling content (ScrollView), adjustResize

There is scrolling content (ScrollView), adjustUnspecified == adjustResize

Input method soft keyboard display control series:

This series of parameters is used to control the display or hiding strategy of the soft keyboard when a thing containing a Window (Activity, Dialog, etc.) is displayed at the forefront of the screen.

stateUnspecified:

The system selects the corresponding mode according to the current specific situation.

stateUnchanged:

The soft keyboard maintains its last state (the state of the soft keyboard when the last Activity or Dialog was at the forefront of the screen) and does not change, regardless of whether the last state was displayed or hidden.

stateHidden:

When the user actively enters the current interface, the soft keyboard is hidden. Leaving the previous interface and returning to the current interface does not count as "active entry". When entering passively, the previous state is maintained.

stateAlwaysHidden:

As long as the user enters this interface, the soft keyboard is hidden, regardless of whether it is active entry (newly launching the interface) or passive entry (leaving the previous interface and returning to the current interface).

stateVisible:

When the user actively enters the current interface, the soft keyboard is displayed. Leaving the previous interface and returning to the current interface does not count as "active entry". When entering passively, the previous state is maintained.

stateAlwaysVisible:

As long as the user enters this interface, the soft keyboard will be displayed, regardless of whether it is active entry (newly launching this interface) or passive entry (leaving the previous interface and returning to the current interface).

A Bug on Some Huawei Phones

I encountered a bug today. On some Huawei phones, except for the first click on the input box, the adjustPan parameter will take effect (the soft keyboard can pop up the input box normally). From the second time onwards, no matter how you click, the adjustPan parameter will be invalid.

The layout model is roughly as follows:

  1. <RelativeLayout
  2. xmlns:android= "http://schemas.android.com/apk/res/android"  
  3. xmlns:tools= "http://schemas.android.com/tools"  
  4. android:id= "@+id/activity_main"  
  5. android:layout_width= "match_parent"  
  6. android:layout_height= "match_parent"  
  7. tools:context= "cn.hjf.inputtest.MainActivity" >
  8.   
  9. <ScrollView
  10. android:layout_width= "match_parent"  
  11. android:layout_height= "match_parent" >
  12.   
  13. <LinearLayout
  14. android:layout_width= "match_parent"  
  15. android:layout_height= "match_parent"  
  16. android:orientation= "vertical" >
  17.   
  18. < View  
  19. android:layout_width= "match_parent"  
  20. android:layout_height= "400dp"  
  21. android:background= "#2b532b" />
  22.   
  23. <EditText
  24. android:layout_width= "90dp"  
  25. android:layout_height= "wrap_content"  
  26. android:layout_gravity= "center_vertical"  
  27. android:background= "@null"  
  28. android:gravity= "center"  
  29. android:inputType= "numberDecimal"  
  30. android:maxLength= "8"  
  31. android:minWidth= "60dp"  
  32. android:padding= "5dp"  
  33. android:text= "0.00" />

The effect is this:

After a long time of troubleshooting (very hard), I finally found the critical point that triggers the bug, which is the following two sentences:

  1. android:gravity= "center"  
  2. android:inputType= "numberDecimal"  

After a long period of verification, we came to a conclusion: On some Huawei models, in this layout model, if the inputType parameter is set in EditText (not none), then when the gravity value is certain, this problem will occur. The general situation is as follows:

Red means invalid, green means valid (can work normally). When these two parameters are not specified, it can work, because the combination of the default values ​​of these two attributes can work, as shown in the blue block in the figure above. (Note: It does not completely match all situations. If there are similar situations, you can match as needed and view the results)

The default values ​​of these two properties can be found in the source code:

attrs.xml

themes.xml

  1. <item name = "editTextStyle" >@style/Widget.EditText</item>

attrs.xml

  1. <! -- The type of data being placed in a text field, used to help an  
  2. input method decide how to let the user enter text. The constants
  3. here correspond to those defined by  
  4. {@link android.text.InputType}. Generally you can select  
  5. a single value, though some can be combined together as  
  6. Setting this attribute to anything besides indicated.
  7. <var>none</var> also implies that the text is editable. -->  
  8. <attr name = "inputType" >
  9. <! -- There is no content type. The text is not editable. -->  
  10. <flag name = "none" value= "0x00000000" />
  1. /**
  2. * Special content type for   when   no explicit type has been specified.
  3. * This should be interpreted to mean that the target input connection  
  4. * is   not rich, it can not process and show things like candidate text nor
  5. * retrieve the current text, so the input method will need to run in a
  6. * limited "generate key events" mode, if it supports it. Note that some  
  7. * input methods may not support it, for example a voice-based input
  8. * method will likely not be able to generate key events even if this
  9. * flag is   set .
  10. */
  11. public   static final int TYPE_NULL = 0x00000000;

A demand

I encountered a requirement, the general model is: there is an interface with an input box and two buttons, which control the addition and subtraction of numbers in the input box respectively, as shown in the following figure:

[[195692]]

Every time the number in the input box changes, whether it is manually entered or controlled by a button, a refresh will be triggered, and a dialog box will pop up during the refresh.

Then the requirement is that when manually inputting, when the refresh is triggered, the input method is displayed, but after the dialog box is displayed and then disappears, the input method is hidden. When it is controlled by a button, there is no problem, because the input method is hidden during the whole process. So the requirement is that after the dialog box is displayed and then disappears, the input method can maintain the state when the refresh is triggered.

Solution: When setting the current Activity to stateUnchanged, the desired effect is not achieved, because the window where the dialog is located does not have this property set, so the dialog window also needs to be set to stateUnchanged, the method is:

  1. getWindow().setSoftInputMode()

And then it works.

<<:  Aiti Tribe Stories (22): From fleeing from Beijing, Shanghai and Guangzhou to fleeing back to Beijing, Shanghai and Guangzhou

>>:  ARKit & OpenGL ES - ARKit principle and implementation

Recommend

Tangyuan or Yuanxiao? Note that these 4 types of people are advised to eat less!

Today is the Lantern Festival. Are you ready to e...

How to play fission well? Share 2 tips!

If you want to know what is the most popular oper...

Have you reached these four levels of coding?

[[234603]] As a software development engineer, co...

Fun fact: What other uses do pigs have besides providing meat for humans?

Pigs are the most common livestock among all live...

Comic Scroll丨Maizi's Journey

I am an ear of wheat, growing in the Central Plai...

A review of the top 10 most popular marketing events in 2018

Alipay Koi, Yang Chaoyue from "Produce 101&q...