For Android search, change the Enter key on the keyboard to search

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.