MainActivity
public class MainActivity extends AppCompatActivity {
private String[] strings=new String[]{"流感","咳嗽","过敏","发烧","感冒","湿疹","便秘","痔疮","协和","鼻炎","失眠","通风","上火","脚气"};
HotView hotView,history;
EditText editText;
SqliteDao dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dao=new SqliteDao(MainActivity.this);
editText=findViewById(R.id.et);
hotView=findViewById(R.id.hotview);
history=findViewById(R.id.history);
for(String str:strings){
TextView textView=new TextView(MainActivity.this);
textView.setText(str);
textView.setBackgroundResource(R.drawable.edit_bg);
textView.setTextSize(24);
hotView.addView(textView);
}
//查询数据库里的数据
List<Bean> beans = dao.selAll();
if(beans.size()!=0){
for(Bean bean:beans){
TextView textView=new TextView(MainActivity.this);
textView = new TextView(MainActivity.this);
textView.setText(bean.getContent());
textView.setTag(bean.getUuid());
textView.setBackgroundResource(R.drawable.edit_bg);
textView.setTextSize(24);
history.addView(textView);
//长按删除
textView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
String tag= String.valueOf(v.getTag());
Log.i("TEST",tag+"--=-");
dao.del(tag);
history.removeView(v);
return true;
}
});
}
}
//点击搜索图片 添加数据
findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView=new TextView(MainActivity.this);
textView=new TextView(MainActivity.this);
textView.setText(editText.getText().toString());
textView.setBackgroundResource(R.drawable.edit_bg);
textView.setTextSize(24);
//判断数据库里是否有此数据 有不添加 没有就添加
boolean b = dao.sel(editText.getText().toString());
if(!editText.getText().toString().equals("") && b){
UUID uuid=UUID.randomUUID();
textView.setTag(uuid);
Log.i("TEST",String.valueOf(uuid));
dao.add(editText.getText().toString(),String.valueOf(uuid));
history.addView(textView);
}
//长按删除
textView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
String tag= String.valueOf(v.getTag());
dao.del(tag);
history.removeView(v);
return true;
}
});
}
});
//点击清空所有 清空数据库数据同时 视图也清空
findViewById(R.id.delAll).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao.delAll();
history.removeAllViews();
}
});
}
}
HotView
public class HotView extends LinearLayout {
private int mChildMaxHeight;
//边距
private int mVSpace = 20;
private int mHSpace = 20;
public HotView(Context context) {
super(context);
}
public HotView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 拿到父容器推荐的宽和高以及计算模式
int sizeWidth=MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight=MeasureSpec.getSize(heightMeasureSpec);
//测量孩子的大小
measureChildren(widthMeasureSpec,heightMeasureSpec);
//寻找孩子中最高的一个孩子,找到的值会放在mChildMaxHeight变量中
findMaxChildMaxHeight();
//初始化值
int left=0,top=0;
//循环所有的孩子
int childCount=getChildCount();
for(int i=0;i<childCount;i++){
View view=getChildAt(i);
//是否是一行的开头
if(left!=0){
//需要换行了,因为放不下啦
if((left+view.getMeasuredWidth())>sizeWidth){
//计算出下一行的top
top+=mChildMaxHeight+mVSpace;
left=0;
}
}
left+=view.getMeasuredWidth()+mHSpace;
}
setMeasuredDimension(sizeWidth,(top+mChildMaxHeight)>sizeHeight?sizeHeight:top+mChildMaxHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
findMaxChildMaxHeight();
//开始安排孩子的位置
//初始化值
int left=0,top=0;
//循环所有的孩子
int childCount=getChildCount();
for(int i=0;i<childCount;i++){
View view=getChildAt(i);
if(left!=0){
if((left+view.getMeasuredWidth())>getWidth()){
//计算出下一行的top
top+=mChildMaxHeight+mVSpace;
left=0;
}
}
//安排孩子的位置
view.layout(left,top,left+view.getMeasuredWidth(),top+view.getMeasuredHeight());
left+=view.getMeasuredWidth()+mHSpace;
}
}
private void findMaxChildMaxHeight() {
mChildMaxHeight=0;
int childCount=getChildCount();
for(int i=0;i<childCount;i++){
View view=getChildAt(i);
if(view.getMeasuredHeight()>mChildMaxHeight){
mChildMaxHeight=view.getMeasuredHeight();
}
}
}
}
数据库的创建
public class SqliteHelper extends SQLiteOpenHelper {
public SqliteHelper(Context context) {
super(context, "User.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建表
db.execSQL("create table history(id integer primary key autoincrement," +
"content text," +
"uuid text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
数据库数据的修改
public class SqliteDao {
private SQLiteDatabase database;
public SqliteDao(Context context) {
database=new SqliteHelper(context).getWritableDatabase();
}
//添加
public void add(String s, String string){
ContentValues values=new ContentValues();
values.put("content",s);
values.put("uuid",string);
database.insert("history",null,values);
}
//全部删除
public void delAll(){
database.delete("history",null,null);
}
//单个查询
public boolean sel(String string){
Cursor query = database.query("history", null, "content=?", new String[]{string}, null, null, null);
return query.getCount()==0?true:false;
}
//查询全部
public List<Bean> selAll(){
List<Bean> list=new ArrayList<>();
Cursor query = database.query("history", null, null, null, null, null, null);
while(query.moveToNext()){
String content = query.getString(query.getColumnIndex("content"));
String uuid = query.getString(query.getColumnIndex("uuid"));
list.add(new Bean(content,uuid));
}
return list;
}
public void del(String tag) {
database.delete("history","uuid=?",new String[]{tag});
}
}
Bean类
public class Bean {
private String content;
private String uuid;
public Bean(String content, String uuid) {
this.content = content;
this.uuid = uuid;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
}
acitivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/add"
android:layout_width="40dp"
android:layout_height="40dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@drawable/search"
/>
<EditText
android:id="@+id/et"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/add"
/>
</android.support.constraint.ConstraintLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textFontWeight="500"
android:layout_marginLeft="20dp"
android:text="搜索历史"/>
<com.example.day04_demo01.view.HotView
android:id="@+id/history"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
/>
<Button
android:id="@+id/delAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空记录"
android:layout_gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_marginLeft="20dp"
android:text="热门搜索"/>
<com.example.day04_demo01.view.HotView
android:id="@+id/hotview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
/>
</LinearLayout>
单个textview布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:gravity="center"
android:textSize="24sp"
android:layout_weight="1"
/>
</LinearLayout>
edit_bg.xml TextView背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00000000" />
<corners android:radius="10px" />
<stroke
android:width="1px"
android:color="#999999" />
</shape>
效果图