搜索功能(支持全拼,首字母,不区分大小写,关键字变色等)

这篇博客介绍了如何在Android应用中实现搜索功能,包括支持全拼、首字母搜索,同时不区分大小写。通过使用pinyin4j库进行字符串转换,结合特定的计算关键字方法,优化了搜索体验。
摘要由CSDN通过智能技术生成

上次写的可能有人说阅读性太差,所以改成这种编辑了

用到的工具主要是pinyin4j(compile ‘com.belerweb:pinyin4j:2.5.1’),将字符串转化成首字拼写或全拼,然后是计算关键字的方法.
- 1.首先是布局界面,可根据需求变更

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:id="@+id/activity_srearch"
  5.     android:orientation="vertical"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent">
  8.     <LinearLayout
  9.         android:background="@color/white"
  10.         android:paddingTop="@dimen/status_bar_height"
  11.         android:orientation="horizontal"
  12.         android:gravity="center_vertical"
  13.         android:layout_width="match_parent"
  14.         android:layout_height="@dimen/activity_titleHeight">
  15.         <ImageView
  16.             android:id="@+id/iv_left"
  17.             android:padding="10dp"
  18.             android:src="@mipmap/back"
  19.             android:layout_width="wrap_content"
  20.             android:layout_height="wrap_content" />
  21.         <SearchView
  22.             android:gravity="center_vertical"
  23.             android:id="@+id/search_view"
  24.             android:layout_width="match_parent"
  25.             android:layout_height="match_parent"
  26.             android:iconifiedByDefault="false"
  27.             android:imeOptions="actionSearch"
  28.             android:queryHint="搜索" />
  29.     </LinearLayout>
  30.     <ListView
  31.         android:id="@+id/lv_search"
  32.         android:scrollbars="none"
  33.         android:layout_width="match_parent"
  34.         android:layout_height="match_parent"/>
  35. </LinearLayout>
  • 2.activity代码
    1. public class SearchActivity extends AppCompatActivity implements View.OnClickListener, SearchView.OnQueryTextListener, AdapterView.OnItemClickListener {
  2.     private SearchView search_view;
  3.     private ListView lv_search;
  4.     private Set infos;//这里的集合使用set,保证搜索结果的唯一性
  5.     private MyAdapter myAdapter;
  6.     private View iv_left;
  7.     @Override
  8.     public void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_srearch);
  11.         //沉浸式
  12.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  13.             getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  14.             //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
  15.         }
  16.         initView();
  17.         initData();
  18.         initAction();
  19.     }
  20.     public void initView() {
  21.         search_view = (SearchView) findViewById(R.id.search_view);
  22.         int id = search_view.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
  23.         TextView textView = (TextView) search_view.findViewById(id);
  24.         iv_left = findViewById(R.id.iv_left);
  25.         textView.setTextColor(UiUtil.getColor(R.color.blue_style));//字体颜色
  26.         textView.setTextSize(14);//字体、提示字体大小
  27.         textView.setHintTextColor(UiUtil.getColor(R.color.font_color_03));//提示字体颜色
  28.         try {//通过反射来设置背景效果
  29.             Class<?> argClass = search_view.
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值