Android实现历史搜索记录

本文实例为大家分享了Android实现历史搜索记录的具体代码,供大家参考,具体内容如下
【Android实现历史搜索记录】Android实现历史搜索记录
文章图片

在app 的 build.gradle下添加依赖

dependencies { ..... api 'com.hyman:flowlayout-lib:1.1.2'}

XML

JAVA
public class MainActivity extends AppCompatActivity { private Button sure,clear; private TagFlowLayout flow; private EditText edit; private List list; private TextView tv; private LayoutInflater from; private Handler handler=new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg); flow.setAdapter(new TagAdapter(list) {@Overridepublic View getView(FlowLayout parent, int position, String o) { tv= (TextView) from.inflate(R.layout.item,flow,false); tv.setText(o); return tv; }}); }}; @Overrideprotected 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() {@Overridepublic void onClick(View v) {String trim = edit.getText().toString().trim(); list.add(trim); handler.sendEmptyMessageDelayed(1,0); }}); //清空clear.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {list.clear(); handler.sendEmptyMessageDelayed(1,0); }}); }}

item布局

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

    推荐阅读