玩九网络 > 陪玩APP源码下载 > APP必备的搜索功能基础源码

APP必备的搜索功能基础源码

发布来源: 玩九网络 发布人: 玩九网络 时间: 2022-09-19 14:34:17

可能在很多人看来,搜索是个不起眼的小功能,无需花太多时间赘述。但实际上搜索作为一个app的最基础的功能,如果做的不好很容易直接拉低用户的好感度,特别是对于工具类的app来说,这些必须的功能虽然很难做出彩,但一定不能拉胯。

 

今天小编就来和大家分享一下搜索功能的基础源码,帮助大家快速上手。


 顶部SPSearchView

 public class SPSearchView extends LinearLayout { 

 private ImageView backImgv; 

 private EditText searchEdtv; 

 private SPSearchViewListener searchListener;

 private SPSearchViewListener searchListener; 

 /**

  * @param context

  * @param attrs

  */ 

 public SPSearchView(Context context, AttributeSet attrs) { 

 super(context, attrs); 

 View view = LayoutInflater.from(context).inflate(http://doc.docsou.comyout.search_heard_view, this); 

 searchEdtv = (EditText)view.findViewById(R.id.search_edtv); 

 backImgv = (ImageView)view.findViewById(R.id.back_imgv); 

 searchEdtv.setOnClickListener(new View.OnClickListener() { 

 @Override 

 public void onClick(View v) { 

 if (searchListener != null) {

 //searchListener.onSearchBoxClick(searchEdtv.getText().toString()); 

 } 

 } 

 }); 

 backImgv.setOnClickListener(new View.OnClickListener() {

 @Override 

 public void onClick(View v) {

 if (searchListener != null) searchListener.onBackClick(); 

 } 

 }); 

 searchEdtv.setOnEditorActionListener(new TextView.OnEditorActionListener() { 

 @Override 

 public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) { //响应键盘"搜索"键 if (actionId == EditorInfo.IME_ACTION_SEARCH) { 

 notifyStartSearching(textView.getText().toString()); 

 

 return true; 

 } 

 }); 

 } 

 /**

  * 通知监听者进⾏搜索操作

  * @param text

  */ 

 private void notifyStartSearching(String text){

  if (searchListener != null) { 

 searchListener.onSearchBoxClick(text); 

 }

 }

 public void setSearchKey(String searchKey){ 

 if (this.searchEdtv!=null && !SPStringUtils.isEmpty(searchKey)){ 

 this.searchEdtv.setText(searchKey);

 }

 }

 public EditText getSearchEditText(){ 

 return this.searchEdtv; 

 }

 public void setSearchViewListener(SPSearchViewListener listener){ 

 this.searchListener = listener; 

 } 

 public interface SPSearchViewListener{

 public void onBackClick();

 public void onSearchBoxClick(String keyword); 

 public void onSearchBoxClick(String keyword);
 }
 }
 搜索界⾯Activity的实现:
 布局⽂件:
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 android:id="@+id/search_view"
 android:layout_width="match_parent"
 android:layout_height="50dip" />
 android:id="@+id/search_key_listv"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:divider="@color/separator_line"
 android:dividerHeight="1px"
 android:fadingEdge="none"
 android:fastScrollEnabled="false"
 android:footerDividersEnabled="false"
 android:headerDividersEnabled="false"
 android:layout_marginTop="50dip"
 android:layout_marginBottom="50dip"
 android:scrollbars="none"
 android:smoothScrollbar="true" />
 android:id="@+id/search_delete_btn"
 android:layout_width="match_parent"
 android:layout_height="@dimen/height_button"

 android:layout_alignParentBottom="true"

 android:layout_marginLeft="20dip"

 android:layout_marginRight="20dip"

 android:layout_marginBottom="10dip"

 style="@style/textStyle.Normal.black"

 android:text="@string/delete_history"

 android:background="@drawable/tag_button_bg_unchecked"

 android:layout_centerHorizontal="true"/>


 onCreate中设置⾃定义标题

 @Override

 protected void onCreate(Bundle savedInstanceState) {

 requestWindowFeature(Window.FEATURE_NO_TITLE);

 super.onCreate(savedInstanceState);

 }

 搜索记录缓存到SharedPrefreences中 

public void loadKey(){


 mSearchkeys = new ArrayList();

 String searchKey = SPSaveData.getString(this, SPMobileConstants.KEY_SEARCH_KEY); if 

(!SPStringUtils.isEmpty(searchKey)){

 String[] keys = searchKey.split(",");

 if (keys !=null)

 for(int i=0; i< keys.length; i++){

 if (!SPStringUtils.isEmpty(keys[i])){

 mSearchkeys.add(keys[i]);

  }

  }

  }

  }

  public void saveKey(String key){

 String searchKey = SPSaveData.getString(this, SPMobileConstants.KEY_SEARCH_KEY); if

 (!SPStringUtils.isEmpty(searchKey) && !searchKey.contains(key)) {

 searchKey+=","+key;

 }else{

 searchKey = key;

 } 

 SPSaveData.putValue(this, SPMobileConstants.KEY_SEARCH_KEY, searchKey);

 } 


以上内容为玩九网络作者本人原创,未经作者本人同意,禁止转载,否则将追究相关法律责任www.9w9w.cn