布局
<?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:textSize="20sp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:text="搜索历史"/>
<com.lll.flowlayout02.FlowLayout
android:id="@+id/flow_history_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
flow:textSize="20sp"
android:layout_weight="1">
</com.lll.flowlayout02.FlowLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingLeft="20dp"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="热门搜索"/>
<com.lll.flowlayout02.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/btn_bg"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="vivo"
android:background="@drawable/btn_bg"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="oppo"
android:background="@drawable/btn_bg"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="小米"
android:background="@drawable/btn_bg"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="华为"
android:background="@drawable/btn_bg"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="笔记本"
android:background="@drawable/btn_bg"
android:textSize="20sp" />
</com.lll.flowlayout02.FlowLayout>
<Button
android:id="@+id/clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除历史记录"/>
**自定义属性** attrs <?xml version="1.0" encoding="utf-8"?>
Shape
<?xml version="1.0" encoding="utf-8"?><stroke
android:color="@color/colorAccent"
android:width="2dp"></stroke>
**样式** <?xml version="1.0" encoding="utf-8"?>
FlowLayout页面
package com.lll.flowlayout02;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
public class FlowLayout extends FrameLayout {
//设置水平距离
int H_DISTANCE = 20;
//设置竖直距离
int V_DISTANCE = 20;
private float mTextSize;
public FlowLayout(Context context) {
super(context);
}
public FlowLayout(Context context,AttributeSet attrs) {
super(context, attrs);
TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.flow);
mTextSize = array.getDimension(R.styleable.flow_textSize, 0);
}
public FlowLayout(Context context,AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.flow);
mTextSize = array.getDimension(R.styleable.flow_textSize, 0);//得到的值单位是像素
}
public void addTextView(String keys){
//加载xml的布局
TextView textView = (TextView) View.inflate(getContext(), R.layout.flow_item, null);
textView.setText(keys);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,mTextSize);
//设置布局的自适应宽高
LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
//控件设置上布局的参数
textView.setLayoutParams(params);
this.addView(textView);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//获取本控件的宽度
int width = getWidth();
//行数
int row = 0;
//子控件左边的坐标
int disWidth = H_DISTANCE;
for (int i=0;i<getChildCount();i++){
//查找子控件
View view = getChildAt(i);
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();
//判断右边的控件宽度是否超过了屏幕的宽度
if (disWidth + viewWidth > width){
//行数增加
row++;
//还远左边坐标
disWidth = H_DISTANCE;
}
int viewTop = row * viewHeight+row * V_DISTANCE;
view.layout(disWidth,viewTop,disWidth+viewWidth,viewTop+viewHeight);
//记录下一次子控件左边的坐标
disWidth += (viewWidth+H_DISTANCE);
}
}
}
数据库
MySqlite
package com.lll.flowlayout02;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqlite extends SQLiteOpenHelper {
public MySqlite(Context context) {
super(context, “fl.db”, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE fl(personid INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(100))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Dao层
package com.lll.flowlayout02;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class Dao {
private final SQLiteDatabase db;
public Dao(Context context) {
//创建对象
MySqlite mySqlite = new MySqlite(context);
db = mySqlite.getWritableDatabase();
}
//写方法
//增
public long insert(String table, String nullColumnHack, ContentValues values) {
return db.insert(table, nullColumnHack, values);
}
//删
public long delete(String table, String whereClause, String[] whereArgs) {
return db.delete(table, whereClause, whereArgs);
}
//改
public long update(String table, ContentValues values, String whereClause, String[] whereArgs) {
return db.update(table, values, whereClause, whereArgs);
}
//查
public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy) {
return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
}
}
MainActivity页面
package com.lll.flowlayout02;
import android.content.ContentValues;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText edit;
private Button btn_search;
private Button clear;
private FlowLayout flow_history;
private long insert;
private Dao dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
edit = findViewById(R.id.edit_keys);
flow_history = findViewById(R.id.flow_history_layout);
btn_search = findViewById(R.id.btn_search);
clear = findViewById(R.id.clear);
btn_search.setOnClickListener(this);
clear.setOnClickListener(this);
//创建Dao层
dao = new Dao(MainActivity.this);
//查询数据库
//创建Dao层
Dao dao = new Dao(MainActivity.this);
Cursor query = dao.query("fl", null, null, null, null, null, null);
if (query.moveToFirst()){
do {
String name = query.getString(query.getColumnIndex("name"));
flow_history.addTextView(name);
}while (query.moveToNext());
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_search:
String keys = edit.getText().toString();
if (keys.equals("")||keys == ""){
Toast.makeText(MainActivity.this,"输入框不能为空",Toast.LENGTH_LONG).show();
return;
}
flow_history.addTextView(keys);
//点击搜索将内容添加到数据库
ContentValues values = new ContentValues();
values.put("name",keys);
insert = dao.insert("fl", null, values);
Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
break;
case R.id.clear:
long delete = dao.delete("fl", null, null);
Toast.makeText(this,"删除条数为"+delete,Toast.LENGTH_SHORT).show();
flow_history.removeAllViews();
break;
}
}
}