Article From:https://www.cnblogs.com/ganchuanpu/p/6568997.html

First, take the initiative to get the focus

setFocusable(true);    //  Is it possible to set the focus. .RequestFocus (); and the control focus.The role of requestFocusInWindow () / / should be to get the focus of the window.

 

Two. Whether the subclass gets the focus

  A common problem in development is that the listview in the project is not just a simple text. It often needs to define the listview itself, its own Adapter to inherit BaseAdapter, and write in the adapter, the problem appears.It is possible to click on every item when there is no response and can not get the focus. The reason is mostly due to the existence of sub controls such as ImageButton, Button, CheckBox and so on in your own definition of Item (also known as Button or C).Heckable’s subclass controls) when these subcontrols get the focus, so often when clicking item is a child control, and the click of the item itself does not respond.

DescendantFocusability can be used to solve this problem. API is described as follows:
This property is the relationship between viewGroup and its child controls when a focus is captured for view.

There are three kinds of values for attributes:
        beforeDescendants:viewgroupFocus on its subclass controls and get the focus
        afterDescendants:viewgroupThe focus is only obtained when its subclass control does not need to capture the focus.
        blocksDescendants:viewgroupOverlay the subclass controls and get the focus directly
We usually use third kinds, that is, the root layout of the Item layout plus the Android:descendantFocusability= “blocksDescendants” attribute.

 

Three. Monitor activity to gain / lose focus (onWindowFocusChanged)
  Effect: 1., you can monitor activity to get / lose focus [hasFocus:true get focus, false lose focus]
           2.Determine the completion of activity loading (when all life cycles are completed, call this method).

@Override 
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub 
    super.onWindowFocusChanged(hasFocus); 
} 

  

Leave a Reply

Your email address will not be published. Required fields are marked *