activity_main.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/et"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"/>
</LinearLayout>
<TextView
android:text="热门搜索"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"/>
<com.bawie.liu.liushibuju4.FlowLayout
android:id="@+id/flowLayout"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.bawie.liu.liushibuju4.FlowLayout>
<TextView
android:text="推荐搜索"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"/>
<com.bawie.liu.liushibuju4.FlowLayout
android:id="@+id/flowLayout2"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="三星"/>
<TextView
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="魅族"/>
<TextView
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="华为"/>
<TextView
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小米"/>
</com.bawie.liu.liushibuju4.FlowLayout>
<Button
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除历史"
tools:ignore="OnClick" />
</LinearLayout>
自定义布局
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
public class FlowLayout extends FrameLayout {
int l=20;
int t=20;
public FlowLayout( Context context) {
super(context);
}
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FlowLayout( Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
//点击搜索将输入框的数据添加进去
public void addTextView(String keys){
//加载布局
TextView textView =(TextView) View.inflate(getContext(),R.layout.flow_item,null);
//设置文字
textView.setText(keys);
//设置文字自适应
LayoutParams layoutParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(layoutParams);
//添加视图
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=l;
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=l;
}
//设置高度
int topView = viewHeight*row+row*t;
//设置位置
view.layout(disWidth,topView,disWidth+viewWidth,topView+viewHeight);
//记录下一次子控件的坐标
disWidth+=(viewWidth+l);
}
}
}
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dao = new Dao(this);
et = findViewById(R.id.et);
bt = findViewById(R.id.bt);
flowLayout = findViewById(R.id.flowLayout);
bt.setOnClickListener(this);
query = dao.query("student", null, null, null, null, null, null);
if (query.moveToFirst()){
while (query.moveToNext()){
String s1 = query.getString(query.getColumnIndex("s"));
flowLayout.addTextView(s1);
}
}else {
Toast.makeText(this, "无数据!!!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.bt){
String s = et.getText().toString();
flowLayout.addTextView(s);
ContentValues values = new ContentValues();
values.put("s",s);
dao.insert("student",null,values);
Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
}else {
et.getText().clear();
flowLayout.removeAllViews();
dao.delete("student",null,null);
Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
}
}
}
自定义布局里的引入的布局
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:textSize="20sp">
</TextView>
创建数据库
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLite extends SQLiteOpenHelper {
public SQLite( Context context) {
super(context, "user.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table student(id Integer primary key autoincrement ,s text )");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
增删查方法
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class dao {
private SQLiteDatabase db;
public dao(Context context){
SQLite sqLite = new SQLite(context);
db = sqLite.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 Cursor cursor(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy){
return db.query(table,columns,selection,selectionArgs,groupBy,having,orderBy);
}
}