1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android 多条件搜索界面 SearchView实现搜索功能

android 多条件搜索界面 SearchView实现搜索功能

时间:2024-02-14 09:03:22

相关推荐

android 多条件搜索界面 SearchView实现搜索功能

SearchView是安卓自带的搜索控件,可以帮助我们省下很多功夫。SearchView提供的api很多,但是麻烦在于SearchView的默认样式很多情况下不满足我们的开发需求,需要我们进行去进行定制。

对于SearchView的定制,查了网上一些资料,自己整理了一下,主要是xml和代码实现两种方式。我使用的是代码实现的方式。通过查找到SearchView控件里的各个子控件的view的Id,再分别属性修改。下面贴一下我的代码,里面的drawable和mipmap等可以替换为自己的资源。//设置searchview样式

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)

private voidsetSearchView(finalSearchView searchView) {

if(searchView !=null) {

//获取最前面的搜索图标ImageView的id

int imgId = searchView.getContext().getResources().getIdentifier("android:id/search_mag_icon", null, null);

//获取ImageView

ImageView searchButton = (ImageView) searchView.findViewById(imgId);

//设置图片

searchButton.setImageResource(R.mipmap.search_icon);

//不使用默认

searchView.setIconifiedByDefault(false);

searchView.setQueryHint("搜索商品");

//设置嵌套有searchicon和searchtext的ll的id

intsearch_edit_ll_id = searchView.getContext().getResources().getIdentifier("android:id/search_edit_frame", null, null);

LinearLayout search_edit_ll = (LinearLayout) searchView.findViewById(search_edit_ll_id);

LinearLayout.LayoutParams lp =newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);

lp.weight=1;

lp.setMargins(0,0,0,0);

search_edit_ll.setLayoutParams(lp);

//获取嵌套searchtext的ll的id

intsearch_plate_ll_id = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);

LinearLayout search_plate_ll = (LinearLayout) searchView.findViewById(search_plate_ll_id);

search_plate_ll.setBackground(getResources().getDrawable(R.drawable.search_view_ll_bg));

//获取到TextView的ID

intid = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);

//获取到TextView的控件

TextView textView = (TextView) searchView.findViewById(id);

textView.setPadding(0,20,0,0);

//设置字体大小为14sp

textView.setTextSize(PLEX_UNIT_SP,14);

//设置字体颜色

textView.setTextColor(getResources().getColor(R.color.white));

//设置提示文字颜色

textView.setHintTextColor(getResources().getColor(R.color.gray));

//获取到close的控件id...search_close_btn

intclodeid = searchView.getContext().getResources().getIdentifier("android:id/search_close_btn", null, null);

ImageView imageView = (ImageView) searchView.findViewById(clodeid);

imageView.setImageResource(R.mipmap.xrzx_gb);

}

}

一般使用到搜索框时,都需要搜索框下面有一个即时显示搜索信息的列表来展示数据库查询到的数据或者网络请求获取的数据。这个对于SearchView来说也不是事情,在SearchView可以通过searchview.setOnQueryTextChangeListener()来监听searchview里的输入内容变化来进行操作。这个方法需要重写两个方法,onQueryTextSubmit()单击搜索按钮时激发该方法,说到这个方法,一般在searchview的布局文件里添加属性,android:imeOptions="actionSearch" 设置输入法里的按钮变为搜索样式,onQueryTextChange()用户输入字符时激发该方法。

第一个方法我们就不用说了,一般都是点击跳转到搜索结果界面。

第二个方法是重点,在这个方法我们可以实时获取用户输入的内容,从而进行操作。下面贴出我的搜索代码。

privateString input="";

/**

*用户输入字符时激发该方法

*/

@Override

public booleanonQueryTextChange(String newText) {

if(newText !=null&& newText.length() >0) {

input= newText;

//执行相应的查询动作

try{

//这里我使用的开启线程,延迟500毫秒执行,在里面判断一次输入的字符和搜索的字符是否一致,不一致就返回,避免重复搜索。

schedule(newSearchTipThread(newText),500);

}catch(Exception e) {

Log.i("TAG",e.toString());

}

}else{

//用户情况输入内容后清除下面显示控件的数据

search_content_recyclerView.setAdapter(null);

}

return true;

}

//线程池最大容量为10

private ScheduledExecutorService scheduledExecutor= Executors.newScheduledThreadPool(10);

//延迟搜索

publicScheduledFutureschedule(Runnable command, longdelayTimeMills) {

returnscheduledExecutor.schedule(command,delayTimeMills,TimeUnit.MILLISECONDS);

}

@Override

public booleanonOptionsItemSelected(MenuItem item) {

switch(item.getItemId()) {

caseandroid.R.id.home: {

onBackPressed();

return true;

}

}

return false;

}

class SearchTipThread implements Runnable {

private String newText;

public SearchTipThread(String newText) {

this.newText= newText;

}

public voidrun() {

// keep only one thread to load current search tip, u can get data from network here

if(newText!=null&&newText.equals(input)) {

//网络请求数据或者查询数据库,并显示在adapter上。

doSearch();

}

}

}

到这里基本就结束了,后续就是把搜索的内容储存到数据库。这个在网上也有很多,也很简单,基本就是执行搜索操作时进行储存,网络上也很多资料,就不赘述了!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。