本文实例为大家分享了FlowLayout实现搜索清空历史记录的具体代码,供大家参考,具体内容如下

效果图:点击搜索框将搜索的历史在流式布局中展示出来,清空历史记录就会将历史清空,每次搜索后都存入sp中,每次进入页面都先判断sp里是否有值并展示

首先需要导入一个module,下载地址

下载完这个工程后,需要将里面的flowlayout-lib导入到工程中,

导入工程的步骤:File - New - Import Module 选中这个flowlayout-lib

导入完成后,在项目的build.gradle中对导入的module进行依赖

compile project(':flowlayout-lib')

activity_main.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="16dp"

tools:context="com.example.searchhistory.MainActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_vertical"

android:orientation="horizontal">

android:id="@+id/edt"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="4" />

android:id="@+id/btn"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="搜索" />

android:id="@+id/clear"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="清空" />

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/id_flowlayout"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

app:max_select="-1" />

tv.xml

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="5dp"

android:layout_marginRight="5dp"

android:layout_marginTop="10dp"

android:background="@drawable/tag_bg"

android:text="Helloworld"

android:textColor="#999999"

android:textSize="16sp">

drawable下面创建

checked_bg.xml

android:width="1dp"

android:color="#dddddd" />

android:bottom="5dp"

android:left="14dp"

android:right="14dp"

android:top="5dp" />

normal_bg.xml

android:width="1dp"

android:color="#dddddd" />

android:bottom="5dp"

android:left="14dp"

android:right="14dp"

android:top="5dp" />

tag_bg.xml

text_color.xml

MainActivity

public class MainActivity extends AppCompatActivity {

private TagFlowLayout mFlowLayout;

private EditText editText;

private Button button;

private List strings;

String history="";

int a=0;

List historylist = new ArrayList<>();

//布局管理器

private LayoutInflater mInflater;

//流式布局的子布局

private TextView tv;

public Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {

switch (msg.what) {

case 1:

mFlowLayout.setAdapter(new TagAdapter(strings) {

@Override

public View getView(FlowLayout parent, int position, String s) {

tv = (TextView) mInflater.inflate(R.layout.tv,

mFlowLayout, false);

tv.setVisibility(View.VISIBLE);

tv.setText(s);

return tv;

}

});

break;

}

super.handleMessage(msg);

}

};

private Button clearbtn;

@RequiresApi(api = Build.VERSION_CODES.M)

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mInflater = LayoutInflater.from(this);

mFlowLayout = (TagFlowLayout) findViewById(R.id.id_flowlayout);

editText = (EditText) findViewById(R.id.edt);

button = (Button) findViewById(R.id.btn);

clearbtn = findViewById(R.id.clear);

final SharedPreferences preferences = getSharedPreferences("config", 0);

final SharedPreferences.Editor editor = preferences.edit();

strings = new ArrayList<>();

final String string = preferences.getString("string", " ");

String[] split = string.split(" ");

if (split.length>0&&!string.equals(" ")){

for (int i=0;i

strings.add(split[i]);

}

handler.sendEmptyMessageDelayed(1, 0);

}

clearbtn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

history="";

historylist.clear();

editor.clear().commit();

//清空下面的

strings.clear();

handler.sendEmptyMessageDelayed(1, 0);

}

});

button.setOnClickListener(new View.OnClickListener() {

@RequiresApi(api = Build.VERSION_CODES.M)

@Override

public void onClick(View v) {

String string = preferences.getString("string", "");

historylist.clear();

if (!editText.getText().toString().trim().equals("")) {

String aa = editText.getText().toString().trim();

Set set = new ArraySet<>();

set.add(aa);

historylist.add(aa);

a++;

history+= aa+" ";

for (int i=0;i

editor.putString("string",history).commit();

strings.add(historylist.get(i));

}

//通知handler更新UI

handler.sendEmptyMessageDelayed(1, 0);

}else{

Toast.makeText(MainActivity.this, "请输入要搜索的内容", Toast.LENGTH_SHORT).show();

}

}

});

//流式布局tag的点击方法

mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {

@Override

public boolean onTagClick(View view, int position, FlowLayout parent) {

Toast.makeText(MainActivity.this, tv.getText(), Toast.LENGTH_SHORT).show();

return true;

}

});

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

android 搜索历史流布局,FlowLayout流式布局实现搜索清空历史记录相关推荐

  1. FlowLayout流式布局实现搜索清空历史记录

    效果图:点击搜索框将搜索的历史在流式布局中展示出来,清空历史记录就会将历史清空,每次搜索后都存入sp中,每次进入页面都先判断sp里是否有值并展示 首先需要导入一个module,下载地址:https:/ ...

  2. 自定义 FlowLayout流式布局搜索框 加 GreenDao存取搜索记录,使用RecyclerView展示

    输入框布局的shape <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android ...

  3. Android FlowLayout 流式布局

    FlowLayout 流式布局 Android 流式布局控件,实现自动换行,操出范围可以滑动功能,未使用控件复用功能,所以不应该有太多的子控件. 主要包含功能: 流式布局,自动换行 使用Adapter ...

  4. android实现标签功能,Android实现热门标签的流式布局

    一.概述: 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧(源码下载在下面最后给出) 类似的 ...

  5. Android 自定义UI 实战 02 流式布局

    Android 自定义UI 实战 02 流式布局-- 自定义ViewGroup 第二章 自定义ViewGroup 流式布局 文章目录 Android 自定义UI 实战 02 流式布局-- 自定义Vie ...

  6. Android自定义View之实现流式布局

    Android自定义View之实现流式布局 运行效果 流式布局 把子控件从左到右摆放,如果一行放不下,自动放到下一行 自定义布局流程 1. 自定义属性:声明,设置,解析获取自定义值 在attr.xml ...

  7. android自定义view流布局,Android控件进阶-自定义流式布局和热门标签控件

    一.概述: 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧 类似的自定义换行流式布局控件.下 ...

  8. Android FlowLayout流式布局

    最近使用APP的时候经常看到有 这种流式布局 ,今天我就跟大家一起来动手撸一个这种自定义控件. 首先说一下自定义控件的流程: 自定义控件一般要么继承View要么继承ViewGroup View的自定义 ...

  9. Flowlayout流式布局使用(轻量级)

    Flowlayout属于自定义流式布局,意思就是说从左上角开始添加原件,依次往后排,第一行挤满了就换一行接着排. 本文所使用的FlowLayout来自于鸿洋大神的框架. 只取了一个自定义控件,没有鸿洋 ...

最新文章

  1. vue 判断页面加载完成_vue项目搭建及总结
  2. Kafka科普系列 | 轻松理解Kafka中的延时操作
  3. 启动数据库报错(2)控制文件丢失
  4. 事务的状态(状态模式)
  5. 更改文件和目录(及子目录)的拥有者
  6. C Primer Plus 第9章 函数 9.4 多源代码文件程序的编译
  7. oracle在指定列后添加列,oracle添加列到指定位置
  8. 大数据_Flink_数据处理_运行时架构7_程序结构和数据流图---Flink工作笔记0022
  9. 【转】从Chrome源码看JS Object的实现
  10. 用java和tomcat安装jenkins过程
  11. ISO/IEC 27000官方文档
  12. LayaIDE + FGUI + Laya-SimpleFramework-Fairygui框架
  13. 2-10-Mysql认实和搭建LAMP环境部署Ucenter和Ucenter-home网站
  14. 基于阿里云的应用系统三级等保1.0测评总结
  15. UG NX 三维PMI标注培训
  16. 用httpUrlConnection实现文件上传
  17. Data Masking-克隆“真实”的假数据
  18. EXCEL破解VBA密码(测试有效)
  19. 【每日新闻】企业微信与微信消息互通正式开放内测 | IBM宣布针对云原生应用推出Microclimate开发平台
  20. tnc_pib_tc

热门文章

  1. 2021年怎么申请政府补贴,企业申请补助的方法
  2. J - Cardinal Adjacencies
  3. 3D模型在线查看利器【多种格式】
  4. 太原理工大学软件学院信息安全课程设计DAY4
  5. 太原理工博士点有计算机软件,太原理工大学02700统计学有博士点吗
  6. 2021-2027全球与中国机械,电气和管道工程(MEP)服务市场现状及未来发展趋势
  7. 最新的 java-正则表达式判断手机号,支持虚拟运营商170号段
  8. CRM上线之路 走上了CRM实施顾问-第98天上班 -第21周
  9. 从屌丝到博士:博主80分考博英语分享!
  10. python数据分析实例-python数据分析实战