效果图是这样
先写一个布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/edit_keys"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_search"
android:layout_width="80dp"
android:layout_height="50dp"
android:text="搜索"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingLeft="20dp"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="搜索历史"/>
<com.example.m2.FlowLayout
android:id="@+id/flow_history_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</com.example.m2.FlowLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingLeft="20dp"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="热门搜索"/>
<com.example.m2.FlowLayout
android:id="@+id/flow_hot_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="apple"
android:background="@drawable/car_btn_bg"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="iphone"
android:background="@drawable/car_btn_bg"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="华为"
android:background="@drawable/car_btn_bg"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="小米"
android:background="@drawable/car_btn_bg"
android:textSize="20sp" />
</com.example.m2.FlowLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="清除历史记录"/>
MainActivity页面
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FlowLayout flowLayout;
private Button mSearch;
private EditText mEdit;
private FlowDatabase mFlowDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEdit = findViewById(R.id.edit_keys);
mSearch = findViewById(R.id.btn_search);
flowLayout = findViewById(R.id.flow_history_layout);
mSearch.setOnClickListener(this);
//新建一个数据库
mFlowDB = new FlowDatabase(this);
//查询搜索历史
List<String> data = mFlowDB.query();
for (int i=0;i<data.size();i++){
//流失布局添加历史
flowLayout.addTextView(data.get(i));
}
}
@Override
public void onClick(View v) {
//这个是点击搜索按钮
if (v.getId()==R.id.btn_search){
String keys = mEdit.getText().toString();
//数据库插入搜索历史
mFlowDB.insert(keys);
//流失布局添加搜索内容
flowLayout.addTextView(keys);
}else {
//移除所有的view
flowLayout.removeAllViews();
//数据库清空所有数据
mFlowDB.delete();
}
}
}