Various pits encountered in the development of Android UI

  • 2021-10-27 08:45:54
  • OfStack

1. Soft keyboard hiding problem

Problem Description: After Activity is pressed back to call finish () method, the interface has been destroyed, but the soft keyboard is still on the screen, which makes the Activity currently displayed completely unable to see without input box, which has a very serious visual impact. Try the scheme: Find various ways to hide the soft keyboard, and find it on the Internet. The idea is that when the activity exits, it will call onDestroy method to destroy the interface, and find a way to hide the interface in this method. Find the following method, but it still won't work. I also tried to find all edittext with the base class and then let them lose focus, hiding the soft keyboard.

InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Solution: 1 It is wrong to start tuning the hidden soft keyboard in onDestroy (), because there are two lifecycle methods before onDestroy (). Like the above method of hiding soft keyboard, there is an getCurrentFocus (), and you will definitely not get the right control to get the current focus before onDestroy (). So hiding the soft keyboard in the onPasue () method is effective, but in the onDestroy () method, no matter what method is used, it is invalid. Note: With this method of hiding soft keyboard, it is best to judge as null, otherwise there may be an exception of null pointer. If there is no control in the current interface to get focus, getCurrentFocus () will get 1 null.

InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
if(getCurrentFocus!=null)
im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
Soft keyboard occupies the layout problem, soft keyboard will sometimes cover 1 some controls, then how to jack up the whole interface, so that any controls will not be covered? There are two steps. The first one sets one attribute in activity, as follows. Step 2, add an scrollview to the layout to put the view you want to be jacked up here, and then when the soft keyboard is displayed, it will scroll in scrollview to get space to display the soft keyboard.

<activity Android:windowSoftInputMode="adjustResize">

2. merge tag attention points

The merge tag is only useful if the root layout is FrameLayout, because the root layout of all Android interfaces is FrameLayout, so it can be merged with merge tag. After the merge tag is used, even if there is EditText in the layout, it can't automatically get the focus, so it can only set the focus manually and call requestFocus () method. Or use requestFocus "in the XML layout file. After using it, pay attention to the fact that if you are in the root layout, you can't use LayoutInflater to generate an view, otherwise you will report the following error, because I used this layout with merge in getview () of listview, so it crashed. Add 1 point, inflater () method can be set attach root for true can be resolved, there will be no crash.

 android.view.InflateException: Binary XML file line 
 #2: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
   Caused by: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true

3. Attention points of 3. LinearLayout

Linear layout is horizontal by default, so make good use of weight weight. The top and bottom tags of layout_gravity have no effect if the orientation is set to horizontal. If the direction is set to vertical, left and right have no effect. If you want to put them to the right, you can use the space tag, set the width to 0dp, and set layoutweight to 1 and put them in front of the control.

4. Layout selection

FrameLayout is the simplest layout, and the root layout of all Android interfaces is FrameLayout, which is the fastest loading speed.

The loading speed of LinearLayou layout and RelativeLayout layout needs to be further studied to reach specific conclusions.

Summarize


Related articles: