上次写的可能有人说阅读性太差,所以改成这种编辑了
用到的工具主要是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.