本文实例为大家分享了android实现历史搜索记录的具体代码,供大家参考,具体内容如下
在app 的 build.gradle下添加依赖
dependencies { ..... api 'com.hyman:flowlayout-lib:1.1.2' }
xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 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" tools:context=".mainactivity"> <edittext android:id="@+id/edit" android:layout_width="match_parent" android:hint="请输入你要搜索的内容" android:layout_height="wrap_content" /> <button android:id="@+id/sure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定"/> <button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空"/> <com.zhy.view.flowlayout.tagflowlayout android:id="@+id/flow" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout>
java
public class mainactivity extends appcompatactivity { private button sure,clear; private tagflowlayout flow; private edittext edit; private list<string> list; private textview tv; private layoutinflater from; private handler handler=new handler(){ @override public void handlemessage(message msg) { super.handlemessage(msg); flow.setadapter(new tagadapter<string>(list) { @override public view getview(flowlayout parent, int position, string o) { tv= (textview) from.inflate(r.layout.item,flow,false); tv.settext(o); return tv; } }); } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); sure=findviewbyid(r.id.sure); clear=findviewbyid(r.id.clear); flow= findviewbyid(r.id.flow); edit= findviewbyid(r.id.edit); list=new arraylist<>(); from = layoutinflater.from(this); //确定 sure.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string trim = edit.gettext().tostring().trim(); list.add(trim); handler.sendemptymessagedelayed(1,0); } }); //清空 clear.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { list.clear(); handler.sendemptymessagedelayed(1,0); } }); } }
item布局
<?xml version="1.0" encoding="utf-8"?> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="#dddddd"/>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。