MainActivity
package com.example.week_3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
});
}
}
LoaginActivity
package com.example.week_3;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.List;
import java.util.UUID;
public class LoginActivity extends AppCompatActivity {
private SearchDao dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final WeekLayoutView layoutView = findViewById(R.id.layoutView);
final WeekLayoutView layoutView1 = findViewById(R.id.layoutView1);
dao = new SearchDao(LoginActivity.this);
TitleBar bar = findViewById(R.id.title);
final List<SearchBean> select = dao.select();
for (int i=0;i<select.size();i++){
TextView tv = new TextView(LoginActivity.this);
tv.setTextColor(Color.RED);
tv.setText(select.get(i).getName());
tv.setBackgroundResource(R.drawable.edit_bg);
layoutView.addView(tv);
final int index=i;
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao.del(select.get(index).getUuid());
layoutView.removeView(v);
}
});
}
bar.setOnButtonClickLister(new TitleBar.OnButtonClickLinster() {
@Override
public void onButtonClick(String str) {
UUID uuid = UUID.randomUUID();
TextView tv = new TextView(LoginActivity.this);
tv.setTag(uuid);
tv.setTextColor(Color.RED);
tv.setText(str);
tv.setBackgroundResource(R.drawable.edit_bg);
layoutView.addView(tv);
dao.add(str,uuid.toString());
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao.del(v.getTag().toString());
layoutView.removeView(v);
}
});
}
});
for (int i=0;i<30;i++){
TextView tv = new TextView(LoginActivity.this);
tv.setTextColor(Color.RED);
tv.setText("热门"+i);
tv.setBackgroundResource(R.drawable.edit_bg);
layoutView1.addView(tv);
}
}
}
自定义头布局titleBar
package com.example.week_3;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
public class TitleBar extends LinearLayout {
private Context mContext;
public TitleBar(Context context) {
super(context);
mContext=context;
init();
}
public TitleBar(Context context, AttributeSet attrs) {
super(context, attrs);
mContext=context;
init();
}
public void init(){
View view = View.inflate(mContext,R.layout.tiltebar,null);
final EditText editText = view.findViewById(R.id.search_edit);
view.findViewById(R.id.sreach_image).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mOnButtonClickLinster!=null){
mOnButtonClickLinster.onButtonClick(editText.getText().toString());
}
}
});
addView(view);
}
//定义成员变量
OnButtonClickLinster mOnButtonClickLinster;
public void setOnButtonClickLister(OnButtonClickLinster onButtonClickLister){
mOnButtonClickLinster=onButtonClickLister;
}
//定义接口
public interface OnButtonClickLinster{
void onButtonClick(String str);
}
}
每一个搜索框的布局
package com.example.week_3;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
public class WeekLayoutView extends LinearLayout {
private int mChildMaxHeigth ;
private int mHSpace=20;
private int mVSpace=20;
public WeekLayoutView(Context context) {
super(context);
}
public WeekLayoutView(Context context, AttributeSet attrs) {
super(context, attrs);
}
//定义单个孩子的大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeigth = MeasureSpec.getSize(heightMeasureSpec);
//测量孩子的大小
measureChildren(widthMeasureSpec,heightMeasureSpec);
findChindMaxHeigth();
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+=mChildMaxHeigth+mVSpace;
left=0;
}
}
left+=view.getMeasuredWidth()+mHSpace;
}
setMeasuredDimension(sizeWidth,(top+mChildMaxHeigth)>sizeHeigth?sizeHeigth:top+mChildMaxHeigth);
}
//孩子在整体布局中的大小
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
findChindMaxHeigth();
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+=mChildMaxHeigth+mVSpace;
left=0;
}
}
//???????????????
view.layout(left,top,view.getMeasuredWidth()+left,mChildMaxHeigth+top);
left+=view.getMeasuredWidth()+mHSpace;
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
public void findChindMaxHeigth(){
mChildMaxHeigth=0;
int childCount=getChildCount();
for (int i=0;i<childCount;i++){
View view = getChildAt(i);
if (view.getMeasuredHeight()>mChildMaxHeigth){
mChildMaxHeigth=view.getMeasuredHeight();
}
}
}
}
自定义属性
package com.example.week_3;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
@SuppressLint("AppCompatCustomView")
public class WeekNameViewGroup extends TextView {
public WeekNameViewGroup(Context context) {
super(context);
}
public WeekNameViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.WeekNameViewGroup);
int color = typedArray.getColor(R.styleable.WeekNameViewGroup_textColor, Color.BLACK);
setTextColor(color);
}
}
数据库
package com.example.week_3;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SqlHelper extends SQLiteOpenHelper {
public SqlHelper( Context context) {
super(context, "Search.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table sreachs(id integer primary key autoincrement," +
"name text," +
"uuid text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
dao层
package com.example.week_3;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class SearchDao {
private SQLiteDatabase database;
public SearchDao(Context context){
SqlHelper helper = new SqlHelper(context);
database = helper.getReadableDatabase();
}
public void add(String name ,String uuid){
ContentValues values = new ContentValues();
values.put("name",name);
values.put("uuid",uuid);
database.insert("sreachs",null,values);
}
public void del(String uuid){
database.delete("sreachs","uuid=?" ,new String[]{uuid});
}
public List<SearchBean> select(){
List<SearchBean> list = new ArrayList<>();
Cursor query = database.query("sreachs", null, null, null, null, null, null,null);
while (query.moveToNext()){
String name = query.getString(query.getColumnIndex("name"));
String uuid = query.getString(query.getColumnIndex("uuid"));
SearchBean searchBean = new SearchBean(name,uuid);
list.add(searchBean);
}
return list;
}
}
bean类
package com.example.week_3;
public class SearchBean {
private String name;
private String uuid;
public SearchBean(String name, String uuid) {
this.name = name;
this.uuid = uuid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
}
loaginxml布局
<?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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LoginActivity">
<com.example.week_3.TitleBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
/>
<com.example.week_3.WeekNameViewGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索记录"
app:textColor="#FF99"
/>
<com.example.week_3.WeekLayoutView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layoutView"
/>
<com.example.week_3.WeekNameViewGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="热门搜索"
app:textColor="#FF99"
/>
<com.example.week_3.WeekLayoutView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/layoutView1"
/>
</LinearLayout>
头布局xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/sreach_image"
android:background="@drawable/search"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_edit"
android:layout_toRightOf="@id/sreach_image"
/>
</RelativeLayout>
attrs属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="WeekNameViewGroup">
<attr name="textColor" format="color"/>
</declare-styleable>
</resources>
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>