How to use the Android Searchview search box Component _android

Source: Internet
Author: User

Searchview is a search box component that allows users to enter text in a text box, get input from a listener, and perform actual searches when a user clicks on a search.

This article for everyone to share the Searchview search box components of the use of methods for your reference, the specific content as follows

Effect:

Code Searchactivity.java

Package com.jialianjia.bzw.activity;
Import Android.os.Bundle;
Import Android.widget.ArrayAdapter;
Import Android.annotation.SuppressLint;
Import Android.widget.ListView;
Import Android.widget.SearchView;
Import com.jialianjia.bzw.BaseActivity;
Import COM.JIALIANJIA.BZW.R;
Import Com.lidroid.xutils.ViewUtils;

Import java.util.ArrayList;
 /** * search * Created by GXS on 2016/5/5. */public class Searchactivity extends baseactivity implements searchview.onquerytextlistener{private Searchview Searc
 Hview;
 Private ListView ListView;
 Private arrayadapter<string> Arrayadapter;
 Private arraylist<string> ArrayList = new arraylist<string> ();

 Private object[] names;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.activity_search);
 names = LoadData ();
 Viewutils.inject (this);
 Searchview = (Searchview) Findviewbyid (R.id.searchview);
 ListView = (ListView) Findviewbyid (R.id.listview); ListvIew.setadapter (New arrayadapter<object> (Getapplicationcontext), Android.
 R.layout.simple_expandable_list_item_1, names));
 Searchview.setonquerytextlistener (this);
 Searchview.setsubmitbuttonenabled (FALSE);
 @Override public boolean onquerytextsubmit (String query) {return false;
 @Override public boolean onquerytextchange (String newtext) {object[] obj = Searchitem (NewText);
 Updatelayout (obj);
 return false;
 Public object[] Searchitem (String name) {arraylist<string> msearchlist = new arraylist<string> ();
  for (int i = 0; i < arraylist.size (); i++) {int index = Arraylist.get (i). IndexOf (name);
  There is a matching data if (index!=-1) {Msearchlist.add (Arraylist.get (i));
 } return Msearchlist.toarray (); }//Update data public void Updatelayout (object[] obj) {listview.setadapter (new arrayadapter<object> (Getapplicationco ntext (), Android.
 R.layout.simple_expandable_list_item_1, obj)); }//test data public object[] LoadData () {Arraylist.add("AAA");
 Arraylist.add ("AaB");
 Arraylist.add ("AAC");
 Arraylist.add ("Aad");
 Arraylist.add ("abc");
 Arraylist.add ("ABCD");
 Arraylist.add ("CDF");
 Arraylist.add ("EDA");
 Arraylist.add ("Sdfa");
 Arraylist.add ("Ddda");
 Arraylist.add ("Sssa");
 return Arraylist.toarray ();

 }
}

Layout Activity_search.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent"
 android:layout_height= "match_parent" android:orientation= "vertical" > <android.support.v7.widget.toolbar Android:id= "@+id/toolbar" android:layout_width= "match_parent" android:layout_height= "50DP" android:background= "? Attr/colorprimary "Android:theme=" @style/apptheme.appbaroverlay "android:fitssystemwindows= true"/> < LinearLayout android:layout_width= "match_parent" android:layout_height= "Wrap_content"

 Horizontal "android:layout_margintop=" 5DP "android:layout_marginleft=" 5DP "android:layout_marginright=" 5DP "> <searchview android:id= "@+id/searchview" android:layout_width= wrap_content "android:layout_height=" Wrap_ Content "android:iconifiedbydefault=" false "android:background=" @drawable/shape_search "android:queryhint=" Please enter the content you want to find "/> <button android:layout_width=" wrap_content "android:layout_height=" WRAp_content "android:text=" Back "/> </LinearLayout> <tablerow android:layout_width=" Match_parent "Android: layout_height= "Wrap_content" android:layout_gravity= "right" ></TableRow> <listview android:id= "@+id/" ListView "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:background=" "#969696"
 > </LinearLayout>

You can also refer to: Android Search box Components Searchview Basic use of in-depth study.

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.