For Android search, change the Enter key on the keyboard to search
For Android search, change the Enter key on the keyboard to search
Requirements:
When you click the search button on the Discovery page, a keyboard is displayed, and the "Search" button is displayed in the lower right corner of the keyboard,
After the user inputs the content, click the search button to search for the keyword. When the user does not enter the content
Click the search button to collapse the keyboard and return to the Discovery page.
The specific implementation is as follows:
1. Modify Editview attributes: android: imeOptions = "actionSearch"
When the Editview obtains the focus, change the "enter" key to "Search"
Android: singleLine = "true"
Otherwise, press enter to [Search] and a new line will be entered.
2. mEditSearchUser. setOnKeyListener (new OnKeyListener (){
@ Override
Public boolean onKey (View v, int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_ENTER ){
// Hide the keyboard first
(InputMethodManager) getSystemService (INPUT_METHOD_SERVICE ))
. HideSoftInputFromWindow (SearchUserActivity. this. getCurrentFocus ()
. GetWindowToken (), InputMethodManager. HIDE_NOT_ALWAYS );
// Method used to perform the search operation. You can add the non-null judgment of mEditSearchUser to this method.
Search ();
}
Return false;
}
});
3. // search
Private void search (){
String searchContext = mEditSearchUser. getText (). toString (). trim ();
If (TextUtils. isEmpty (searchContext )){
TipTools. Define toast (this, "the input box is empty. Enter ");
} Else {
// Call the search API method
SearchUser (searchContext );
}
}
This is the case. It is very simple. Try it now!