The basic use method of the Android search box component Searchview _android

Source: Internet
Author: User

Searchview is a built-in search box component in the Android system that can be easily added to the user interface, but it also poses some problems with the Searchview UI being fixed and cumbersome to customize. If the demand for Searchview is higher, it can be implemented by button and EditText. Here is a simple talk about the use of Searchview:

Main.xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
 xmlns:tools= "http:// Schemas.android.com/tools "
 android:layout_width=" match_parent "
 android:layout_height=" Match_parent "
 android:orientation= "vertical"
 tools:context= ". Main ">

 <searchview
  android:id=" @+id/sv "
  android:layout_width=" Wrap_content "
  android: layout_height= "Wrap_content"
  android:imeoptions= "Actiongo"/>

</LinearLayout>

The following layout file is used when displaying the suggestion: Mytextview.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
 android:layout_width=" match_parent "
 android:layout_height=" 50sp "
 android:orientation= "Vertical" >

 <textview
  android:id= "@+id/textview"
  android:layout_width= "Match_parent"
  android:layout_height= "Wrap_content"
  android:gravity= "center_vertical"
  android:paddingleft= "5SP"
  android:textsize= "18sp"/>

</LinearLayout>

Main.java:

Package com.app.main;

Import Java.lang.reflect.Field;
Import android.app.Activity;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.SearchView;
Import Android.widget.SearchView.OnQueryTextListener;
Import Android.widget.SimpleCursorAdapter;

Import Android.widget.Toast;
 public class Main extends activity {Searchview SV = null;

 ListView LV = null;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

  Setcontentview (R.layout.main);

  SV = (Searchview) This.findviewbyid (R.ID.SV);

  Sv.seticonifiedbydefault (FALSE);

  Sv.setsubmitbuttonenabled (TRUE);

   Sv.setqueryhint ("Query"); By reflecting, modifying the default style, you can find the required components from the Android Search_view.xml the try {field field = Sv.getclass (). Getdeclaredfield ("Msubmitbutto

   n ");

   Field.setaccessible (TRUE); ImageView IV = (ImageView) Field.get (SV);


  Iv.setimagedrawable (This.getresources (). getdrawable (R.drawable.pointer));
  catch (Exception e) {e.printstacktrace ();

  } Cursor Cursor = This.gettestcursor (); @SuppressWarnings ("deprecation") simplecursoradapter adapter = new Simplecursoradapter (this, R.layout.mytextview, CU

  Rsor, new string[] {"Tb_name"}, new int[] {r.id.textview});

  Sv.setsuggestionsadapter (adapter);

    Sv.setonquerytextlistener (New Onquerytextlistener () {@Override public boolean onquerytextchange (String str) {
   return false; @Override public boolean onquerytextsubmit (String str) {toast.maketext (main.this, str, toast.length_short).

    Show ();
   return false;

 }

  }); }//Add suggestion required data public Cursor Gettestcursor () {Sqlitedatabase db = Sqlitedatabase.openorcreatedatabase (t

  His.getfilesdir () + "/MY.DB3", null);
  Cursor Cursor = null;

   try {String insertsql = "INSERT into tb_test values (null,?,?)"; Db.Execsql (Insertsql, new object[] {"AA", 1});

   Db.execsql (Insertsql, new object[] {"AB", 2});

   Db.execsql (Insertsql, new object[] {"AC", 3});

   Db.execsql (Insertsql, new object[] {"Ad", 4});

   Db.execsql (Insertsql, new object[] {"AE", 5});

   String querysql = "SELECT * from Tb_test";

  cursor = db.rawquery (querysql, NULL); The catch (Exception e) {String sql = "CREATE TABLE tb_test" (_id integer PRIMARY key autoincrement,tb_name varchar (20),

   Tb_age integer) ";

   Db.execsql (SQL);

   String insertsql = "INSERT into tb_test values (null,?,?)";

   Db.execsql (Insertsql, new object[] {"AA", 1});

   Db.execsql (Insertsql, new object[] {"AB", 2});

   Db.execsql (Insertsql, new object[] {"AC", 3});

   Db.execsql (Insertsql, new object[] {"Ad", 4});

   Db.execsql (Insertsql, new object[] {"AE", 5});

   String querysql = "SELECT * from Tb_test";
  cursor = db.rawquery (querysql, NULL);
 return cursor;

 }

}

The effect of the implementation is as follows:

The above is the search box component Searchview Basic use method, hope can give everybody a reference, also hope that we support cloud habitat community a lot.

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.