Objective
Searchview is commonly used in Android search control, there are many online about Searchview are Java, so I refer to the Xamaroin official website Some of the demo to summarize some methods.
Guide
1. How to create a Searchview
Common events for 2.searchview
3. How to use Searchview
Body
1. How to create a Searchview
creating a Searchview is simple, we just need to create a new layout file and rewrite the oncreateoptionsmenu.
, create a new folder named menu under the Resources folder and create a new XML layout file within the folder.
Search.xml
<?XML version= "1.0" encoding= "Utf-8"?> <Menuxmlns:android= "Http://schemas.android.com/apk/res/android"> <ItemAndroid:id= "@+id/search"Android:title= "Search"android:showasaction= "Ifroom"Android:actionviewclass= "Android.widget.SearchView" /> </Menu>
so our layout file is built, and then rewrite the oncreateoptionsmenu.
Public Override BOOL oncreateoptionsmenu (Imenu menu) { menuinflater.inflate (Resource.Menu.search, Menu); var searchview = (Searchview) menu. FindItem (Resource.Id.search). Actionview; return true ; }
The end result is as follows:
This way our searchview is created.
The Main method of 2.searchview
The role of Searchview is to search, and the events we commonly use include the following:
Onquerytextlistener called when the query content has changed
Oncloselistener Called when Searchview is closed
Onquerytextchange The new content of the text field of the query, which is equivalent to writing edge search
onquerytextsubmit Querying the content of the query to be submitted
onClose events that are triggered when Searchview is turned off
Here's how you can use the code to illustrate the usage of these kinds of events
3. How to use Searchview
Call this method first to inherit the Searchview class
Public class Activity1:searchview.ionquerytextlistener,searchview.ioncloselistener
This also inherits Ionquerytextlistener and Ioncloselistener, and if you do not inherit this class, you will be prompted for an error that the type cannot be converted.
After the inheritance we can set the listener event, also rewrite the oncreateoptionsmenu.
Searchview.setonquerytextlistener (this), Searchview.setoncloselistener (this);
after setting up the listener event, you can override these methods
Public BOOLOnquerytextchange (stringNewText) { return true; } Public BOOLOnquerytextsubmit (stringquery) { return true; } Public BOOLOnClose () {return true; }
Photo Demo
Some uses of Xamarin Android Searchview