1 Star 0 Fork 1

独毒火 / Search_Layout

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

SearchLayout

Download

  • 原作者:Carson_Ho
  • 修改者:LiYing(独毒火)

注:关于该开源项目的意见 & 建议可在 Issue 上提出。欢迎 Star !

1. 简介

一款封装了 历史搜索记录功能 & 样式Android 自定义搜索框。

已在 Github 开源:地址:SearchLayout,欢迎 Star

Demo

2. 功能介绍

2.1 需求场景

  1. 主要: 对某类事物进行精确搜索;
  2. 次要: 降低二次搜索的操作成本。

2.2 功能需求

  • 功能列表
需求场景 功能 描述 优先级
主要)对某类事物进行精确搜索 关键字搜索 根据用户的关键字输入进行数据库查找 P1
次要)降低二次搜索的操作成本 实时显示历史搜索记录 包括:最近搜索记录 & 相似搜索记录(根据用户输入) P2
保存历史搜索记录 将用户的搜索字段进行保存(以点击搜索按钮为准) P3
删除历史搜索记录 将数据库中保存的历史搜索记录删除或清空 P4
一键删除输入搜索关键词 将用户当前输入的搜索关键词清空 P5
  • 功能原型

Prototype

3. 特点

3.1 功能实用

  • 该搜索框开源库具备除了历史搜索记录功能外,还具备一般的搜索框功能(如一键清空搜索框内容等等)
  • 封装了 常见的搜索框样式(如图标、字体、背景等等),使用起来更加方便

3.2 使用简单

仅需要简单的 xml 属性配置

下面1节会详细介绍其使用方法

3.3 二次开发成本低

4. 具体使用

方式1:直接使用控件 SearchView

步骤1:引入控件库

主要有 Gradle & Maven 2种方式:

  • 方式1:Gradle引入依赖

build.gradle

dependencies {
    implementation 'cc.duduhuo:search-view:1.1.6'
}
  • 方式2:Maven引入依赖

pom.xml

<dependency>
  <groupId>cc.duduhuo</groupId>
  <artifactId>search-view</artifactId>
  <version>1.1.6</version>
  <type>pom</type>
</dependency>

步骤2:设置搜索框样式

  • 自定义属性列表
属性 描述 类型 默认值
searchTextSize 搜索字体大小 dimension 14sp
searchTextColor 搜索字体颜色 color #9B9B9B
searchTextHint 搜索框编辑框提示内容 string 输入查询关键字
searchTextBackground 搜索编辑框背景 reference 0
searchBlockColor 搜索控件背景颜色 color #FFFFFF
searchBlockBackground 搜索控件背景 reference -
searchBlockHeight 搜索控件高度 dimension 46dp
searchButtonBackground 搜索按钮背景 reference 0
searchButtonIconColor 搜索按钮图标颜色 color #878787
searchButtonVisible 搜索按钮是否可见 boolean true
iconColor 所有图标的颜色 color #878787
backIconColor 返回图标的颜色 color #878787
searchIconColor 搜索图标的颜色 color #878787
deleteIconColor 删除图标的颜色 color #878787
searchIconVisible 搜索图标是否可见 boolean false
clearHistoryText 清除历史记录文字 string 清空搜索历史
clearHistoryTextColor 清除历史记录文字颜色 color #606060
clearHistoryTextSize 清除历史记录文字大小 dimension 14sp
clearHistoryTextBackground 清除历史记录文字背景 reference pressed: #ececec
normal: #e2e2e2
  • 使用示例

在 layout 文件中使用,如:

<scut.carson_ho.searchview.SearchView
    android:id="@+id/search_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:clearHistoryText="清空搜索历史"/>

步骤3:设置点击键盘上的搜索按键 & 返回按键后的操作

private SearchView searchView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    // 搜索框组件
    searchView = findViewById(R.id.search_view);
    // 是否在点击历史条目后启动搜索
    searchView.startSearchWhenHistoryItemClick = true;
    // 设置点击搜索按键后的操作(通过回调接口)
    // 参数 = 搜索框输入的内容
    searchView.setOnSearchListener(new OnSearchListener() {
        @Override
        public void onSearch(String keyword) {
            Toast.makeText(SearchViewDemoActivity.this, "搜索关键词:" + keyword, Toast.LENGTH_SHORT).show();
            // 也可通过 getSearchText() 方法获取搜索框中的内容
            String searchText = searchView.getSearchText();
            System.out.println(searchText);
            // 如果需要自定义搜索按钮,可以在按钮的click事件中调用 startSearch() 方法。
            // searchView.startSearch();
        }
    });
    // 设置点击返回按键后的操作(通过回调接口)
    searchView.setOnBackListener(new OnBackListener() {
        @Override
        public void onBack() {
            finish();
        }
    });
}

方式2:使用控件 SearchFragment

步骤1:实例化 SearchFragment

SearchFragment searchFragment = SearchFragment.newInstance();

步骤2:设置搜索回调的监听

searchFragment.setOnSearchListener(new OnSearchListener() {
    @Override
    public void onSearch(String keyword) {
        Toast.makeText(SearchFragmentDemoActivity.this, "搜索关键词:" + keyword, Toast.LENGTH_SHORT).show();
    }
});

步骤3:显示 SearchFragment

searchFragment.show(getSupportFragmentManager(), SearchFragment.TAG);

5. 完整Demo地址

liying2008的Github地址:SearchLayout

6. 开源协议

MIT

7. 贡献代码

  • 具体请看:贡献代码说明
  • 关于该开源项目的意见 & 建议可在 Issue 上提出。欢迎 Star & Fork !

8. 版本说明

版本更新说明

关于原作者

关于LiYing(独毒火)

MIT License Copyright (c) 2017 何家成 Copyright (c) 2018 LiYing Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一款封装了 历史搜索记录功能 & 样式 的Android自定义搜索框(改良版) 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/neuliying/Search_Layout.git
git@gitee.com:neuliying/Search_Layout.git
neuliying
Search_Layout
Search_Layout
master

搜索帮助