is to implement the search box at the top.
The search box is an item on the menu on the Actionbar. Called Searchview. We can first define it in the menu option:
Bmap_menu.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/menu_search" android:icon= "@android:d rawable/ic_menu_search" android:title= "@string/action_search" android:actionviewclass= "Android.widget.SearchView" Android : showasaction= "Ifroom|collapseactionview"/></menu>
The Showasaction Collapseactionview here indicates that Searchview is allowed to be extended to the entire actionbar.
Second, the configuration searchble:
Search View can be seen as the identification of a quick entry for seachble activity in Acionbar. Then we can configure the relevant information by configuring the searchble:
Under the Res/xml folder (if you don't have an XML folder, create a new one), create a new file: Searchable.xml
<?xml version= "1.0" encoding= "Utf-8"? ><searchable xmlns:android= "http://schemas.android.com/apk/res/ Android " android:label=" @string/search_label " android:hint=" @string/search_hint "/>
Third, configuration Androidmanifest.xml
Since Searchview is an ingress identifier, you need to configure filtering in the Androidmanifest.xml file for a long time:
<activity android:name= "cn.com.smartcost.cld.ui.BmapActivity" > <intent-filter> < Action android:name= "Android.intent.action.SEARCH"/> </intent-filter>
Iv. Searchview Loading searchble configuration information:
public boolean Oncreateoptionsmenu (Menu menu) {//Joins the menu containing search view Menuinflater Inflater = Getmenuinflater (); Inflater.inflate (menuId, menu); Gets the Searchview object Searchview Searchview = (searchview) menu.finditem (r.id.menu_search). Getactionview (); if (Searchview = = null) {LOG.E ("Searchview", "Fail to get Search View."); return true; } Searchview.seticonifiedbydefault (True); The default value is true, may not be set specifically, false and true as follows, true input box greater//Get Search service Manager Searchmanager Searchmanager = (Searchmanager) gets Ystemservice (Context.search_service); The component name of the searchable activity, by which the system can evoke componentname CN = new ComponentName (this,searchresultactvity) through intent. Class); With the search manager, obtaining relevant search information from searchable activity is the searchable XML setting. If NULL is returned, indicates that the activity does not exist, or is not searchable searchableinfo info = Searchmanager.getsearchableinfo (CN); if (info = = null) {LOG.E ("Searchableinfo", Fail to get search info. "); Will searchable activiTy's search information is associated with the Searchview.setsearchableinfo (info). return true; }V. Response to search activity:
public class Bmapactivity extends activity{ @Override protected void onCreate (Bundle savedinstancestate) { ... Dosearchquery (Getintent ()); } @Override protected void onnewintent (Intent Intent) { //activity re -pinned Super.onnewintent (Intent); Dosearchquery (intent); } The call to searchable activity is still a standard Intent, and we can get information from Intent, that is, the content to be searched private void Dosearchquery (Intent Intent) { if (intent = = null) return; String queryaction = Intent.getaction (); if (Intent.ACTION_SEARCH.equals (Intent.getaction ())) { //if it is called through Action_search, that is, if a String is called through a search queryString = Intent.getstringextra (searchmanager.query); Get search content ... } ... ... }
In addition, there are several requirements:
1. How to change the color of the horizontal line under the default search box, this seems easy, in fact, very troublesome, if you set the background image directly, it is likely to cause superposition of the situation. On the Internet to see the overflow forum of the people's methods, I think it is not ideal, some people put forward to use reflection method to solve, I also use reflection approach:
Try{class<?> Argclass=searchview.getclass (); Field Ownfield = Argclass.getdeclaredfield ("Msearchplate"); Setaccessible ownfield.setaccessible (true); View MView = (view) ownfield.get (Searchview); Mview.setbackground (Getresources (). getdrawable (R.drawable.test)); } catch (Exception e) {e.printstacktrace ();}
But there's a problem, if your searchview has a submit, then it's just the horizontal line under the search box is discolored, and the color of the submit is still blue. At the moment, I can't find the name of the member variable called submit. I wonder if you have any good ideas.
2. Expand Searchview By default:
A lot of people have this need, want to default Searchview expand, and then add the default text on top (not hint).
This uses this to unfold:
MenuItem Searchitem = Menu.finditem (R.id.menu_search); Searchitem.expandactionview ();
This sets the text:
Searchview.setquery ("OK", false);
If only it would. The virtual keyboard also opens, which affects the experience, and the way to turn off the virtual keyboard is to make Searview clear the focus:
Searchview.clearfocus ();
Reference:
Google Development Documentation: http://developer.android.com/training/search/setup.html
Kevin Feng's blog http://blog.csdn.net/flowingflying/article/details/14163401