Android ENTER to send the message, and soft keyboard Collapse

When you click the input box input, the input completion time, tried to hide the soft keyboard directly by clicking directly on the Enter key soft keyboard, and send content or search for content.

1. First xml disposed in a place, add a property :

<EditText
     android:id="@+id/et_title"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:imeOptions="actionSearch"
     android:singleLine="true" />
The default soft keyboard buttons to the bottom right corner of the case, "next", click to the next input box will be set imeOptions value equal to actionSearch, actionDone, actionSend, etc., under the soft keyboard into a "search", "Finish", "Finish "Wait. (Note: Maybe a little time "search" is not necessarily expressed in words, but a search icon, which is a magnifying glass)
The above are just a few examples, as well as readers can go try. 
2. The next step is to monitor the keyboard's Enter key:

EditText achieve setOnEditorActionListener, in onEditAction method actionId on the corresponding imeOptions we set. The system default actionId have: EditorInfo.IME_NULL, EditorInfo.IME_ACTION_SEND, EditorInfo.IME_ACTION_DONE and so on.

the findViewById (R.id.et_title) .setOnEditorActionListener ( new new TextView.OnEditorActionListener () { 
            @Override 
            public  Boolean onEditorAction (the TextView V, int actionId, KeyEvent Event) {
                 IF (actionId == EditorInfo.IME_ACTION_SEARCH) {
                     // search (send message ) operation (omitted here, according to the reader's own needs, directly call the method they want to achieve) 
// hide the soft keyboard InputMethodManager IM = (InputMethodManager) mContext.getSystemService (Context.INPUT_METHOD_SERVICE); 
                    im.hideSoftInputFromWindow (getActivity () .getCurrentFocus () getWindowToken (), InputMethodManager.HIDE_NOT_ALWAYS);. 
                } return                   
false; } });

When entering a carriage return should also put away the soft keyboard, then carry on a little point display the soft keyboard and hide it

IMM = InputMethodManager (InputMethodManager) mContext.getSystemService (Context.INPUT_METHOD_SERVICE); 

// Get a soft keyboard display state
Boolean imm.isActive the isOpen = ();

// if the soft keyboard has been displayed, hidden, otherwise display
imm.toggleSoftInput ( 0, InputMethodManager.HIDE_NOT_ALWAYS);

// hide the soft keyboard
imm.hideSoftInputFromWindow (getActivity () getCurrentFocus () getWindowToken (), InputMethodManager.HIDE_NOT_ALWAYS);..

// forced to display the soft keyboard
imm.showSoftInput (getActivity () getCurrentFocus () . , InputMethodManager.SHOW_FORCED);

// forced to hide the soft keyboard
imm.hideSoftInputFromWindow (getActivity () getCurrentFocus () getWindowToken (), 0)..;

 

 

 

 

Guess you like

Origin www.cnblogs.com/huiing/p/12069123.html