示例APK可从这些地址下载:Google Play, 360手机助手, 百度手机助手, 小米应用商店, 豌豆荚

1. layout文件

Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns : android = "http://schemas.android.com/apk/res/android" android : layout_width = "match_parent" android : layout_height = "match_parent" > < SearchView android : id = "@+id/search_view" android : layout_width = "match_parent" android : layout_height = "match_parent" android : iconifiedByDefault = "true" android : inputType = "textCapWords" android : imeOptions = "actionSearch" android : queryHint = "" / > < / RelativeLayout >

xml中主要配置有四个属性,如下:

android:iconifiedByDefault表示搜索图标是否在输入框内。true效果更加
android:imeOptions设置IME options,即输入法的回车键的功能,可以是搜索、下一个、发送、完成等等。这里actionSearch表示搜索
android:inputType输入框文本类型,可控制输入法键盘样式,如numberPassword即为数字密码样式
android:queryHint输入框默认文本

2. java部分代码
SearchView几个主要函数
setOnCloseListener(SearchView.OnCloseListener listener)表示点击取消按钮listener,默认点击搜索输入框
setOnQueryTextListener(SearchView.OnQueryTextListener listener)表示输入框文字listener,包括public boolean onQueryTextSubmit(String query)开始搜索listener,public boolean onQueryTextChange(String newText)输入框内容变化listener,两个函数,下面代码包含了如何利用延迟执行实现搜索提示

Java部分实现 Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 package cn . trinea . android . demo ; import java . util . concurrent . Executors ; import java . util . concurrent . ScheduledExecutorService ; import java . util . concurrent . ScheduledFuture ; import java . util . concurrent . TimeUnit ; import android . app . ActionBar ; import android . app . ActionBar . LayoutParams ; import android . app . Activity ; import android . content . Context ; import android . os . Bundle ; import android . os . Handler ; import android . os . Message ; import android . view . Gravity ; import android . view . LayoutInflater ; import android . view . MenuItem ; import android . view . View ; import android . view . WindowManager ; import android . widget . SearchView ; import android . widget . SearchView . OnCloseListener ; import android . widget . Toast ; public class SearchViewDemo extends Activity { private SearchView searchView ; private Context context ; private MyHandler handler ; // schedule executor private ScheduledExecutorService scheduledExecutor = Executors . newScheduledThreadPool ( 10 ) ; private String currentSearchTip ; @Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . search_view_demo ) ; context = getApplicationContext ( ) ; handler = new MyHandler ( ) ; ActionBar actionBar = getActionBar ( ) ; actionBar . setDisplayOptions ( ActionBar . DISPLAY_SHOW_TITLE | ActionBar . DISPLAY_HOME_AS_UP | ActionBar . DISPLAY_SHOW_CUSTOM ) ; setTitle ( " " ) ; LayoutInflater inflater = ( LayoutInflater ) getSystemService ( Context . LAYOUT_INFLATER_SERVICE ) ; View customActionBarView = inflater . inflate ( R . layout . search_view_demo_title , null ) ; searchView = ( SearchView ) customActionBarView . findViewById ( R . id . search_view ) ; searchView . setIconified ( false ) ; searchView . setOnCloseListener ( new OnCloseListener ( ) { @Override public boolean onClose ( ) { // to avoid click x button and the edittext hidden return true ; } } ) ; searchView . setOnQueryTextListener ( new SearchView . OnQueryTextListener ( ) { public boolean onQueryTextSubmit ( String query ) { Toast . makeText ( context , "begin search" , Toast . LENGTH_SHORT ) . show ( ) ; return true ; } public boolean onQueryTextChange ( String newText ) { if ( newText != null && newText . length ( ) > 0 ) { currentSearchTip = newText ; showSearchTip ( newText ) ; } return true ; } } ) ; LayoutParams params = new LayoutParams ( LayoutParams . WRAP_CONTENT , LayoutParams . WRAP_CONTENT , Gravity . CENTER_VERTICAL | Gravity . RIGHT ) ; actionBar . setCustomView ( customActionBarView , params ) ; // show keyboard getWindow ( ) . setSoftInputMode ( WindowManager . LayoutParams . SOFT_INPUT_ADJUST_RESIZE | WindowManager . LayoutParams . SOFT_INPUT_STATE_VISIBLE ) ; } public void showSearchTip ( String newText ) { // excute after 500ms, and when excute, judge current search tip and newText schedule ( new SearchTipThread ( newText ) , 500 ) ; } class SearchTipThread implements Runnable { String newText ; public SearchTipThread ( String newText ) { this . newText = newText ; } public void run ( ) { // keep only one thread to load current search tip, u can get data from network here if ( newText != null && newText . equals ( currentSearchTip ) ) { handler . sendMessage ( handler . obtainMessage ( 1 , newText + " search tip" ) ) ; } } } public ScheduledFuture < ? > schedule ( Runnable command , long delayTimeMills ) { return scheduledExecutor . schedule ( command , delayTimeMills , TimeUnit . MILLISECONDS ) ; } @Override public boolean onOptionsItemSelected ( MenuItem item ) { switch ( item . getItemId ( ) ) { case android . R . id . home : { onBackPressed ( ) ; return true ; } } return false ; } private class MyHandler extends Handler { @Override public void handleMessage ( Message msg ) { switch ( msg . what ) { case 1 : Toast . makeText ( context , ( String ) msg . obj , Toast . LENGTH_SHORT ) . show ( ) ; break ; } } } }

上面代码在onQueryTextChange函数即输入框内容每次变化时将一个数据获取线程SearchTipThread放到ScheduledExecutorService中,500ms后执行,在线程执行时判断当前输入框内容和要搜索内容,若相等则继续执行,否则直接返回,避免不必要的数据获取和多个搜索提示同时出现。

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
| WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
表示默认输入法弹出

编辑框内容为空点击取消的x按钮,编辑框收缩,可在onClose中返回true

Java
1 2 3 4 5 6 7 searchView . setOnCloseListener ( new OnCloseListener ( ) { @Override public boolean onClose ( ) { return true ; } } ) ;

searchView.onActionViewExpanded();表示在内容为空时不显示取消的x按钮,内容不为空时显示.

searchView.setSubmitButtonEnabled(true);编辑框后显示search按钮,个人建议用android:imeOptions=”actionSearch”代替。

隐藏输入法键盘

Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 InputMethodManager inputMethodManager ; inputMethodManager = ( InputMethodManager ) getSystemService ( Context . INPUT_METHOD_SERVICE ) ; private void hideSoftInput ( ) { if ( inputMethodManager != null ) { View v = SearchActivity . this . getCurrentFocus ( ) ; if ( v == null ) { return ; } inputMethodManager . hideSoftInputFromWindow ( v . getWindowToken ( ) , InputMethodManager . HIDE_NOT_ALWAYS ) ; searchView . clearFocus ( ) ; } }

其中SearchActivity为Activity的类名

更多相关文章

  1. 更改android spinner 背景
  2. Android软键盘调用
  3. Android(安卓)TextView文字过多时,通过滚动条显示多余内容
  4. Android(安卓)监听短信接收 获取发件号码、短信内容
  5. android 内容提供者查询单个使用URI匹配器
  6. android am命令
  7. Android(安卓)SDK Manage 无法更新下载内容
  8. Android(安卓)输入法键盘和activity页面遮挡问题解决
  9. Android中ListView.getCount()与ListView.getChildCount()区别和

随机推荐

  1. Android之自定义五子棋View
  2. Android(安卓)Picasso最详细的使用指南
  3. Android(安卓)ApiDemos示例解析(147):Vie
  4. 浅谈android中的反编译
  5. Android屏幕投影及反向控制原理
  6. Android 4.0 Ice Cream Sandwich 正式发
  7. Android基于API的Tabs3实现仿优酷tabhost
  8. Android(安卓)Studio: License for packa
  9. Android app——活动的生命周期
  10. 关于android内存管理的原理,及相关自动内