Xml
<com.bwei.week01.CustomTitleView
android:id="@+id/titleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索记录"
android:textSize="20dp"/>
<com.bwei.week01.CustomFlawView
android:id="@+id/falwView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="热门搜索"
/>
<com.bwei.week01.CustomFlawView
android:id="@+id/reView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
MainActivity
titleView=findViewById(R.id.titleView);
flawView=findViewById(R.id.falwView);
dao=new UserDao(this);
CustomFlawView reView=findViewById(R.id.reView);
titleView.setOnButtonClickListener(new CustomTitleView.setButtonClickListener() {
@Override
public void onSuccess(final String str) {
//随机数当作唯一标识
UUID uuid = UUID.randomUUID();
TextView view = new TextView(MainActivity.this);
view.setTag(uuid);
view.setTextColor(Color.BLACK);
view.setText(str);
uuid1 = String.valueOf(view.getTag());
dao.add(uuid1,str);
view.setBackgroundResource(R.drawable.edit_bg);
flawView.addView(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao.del(uuid1);
flawView.removeView(v);
}
});
}
});
for (int i=0;i<10;i++){
TextView tv = new TextView(MainActivity.this);
tv.setText("数据:" + i);
tv.setTextColor(Color.RED);
tv.setBackgroundResource(R.drawable.edit_bg);
reView.addView(tv);
}
titleView自定义
private Context mContext;
public CustomTitleView(Context context) {
super(context);
mContext=context;
init();
}
public CustomTitleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mContext=context;
init();
}
private void init() {
View view = View.inflate(mContext, R.layout.activity_title, null);
final EditText editText = view.findViewById(R.id.editText);
ImageView imageView = view.findViewById(R.id.imageView);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(setButtonClickListener!=null){
setButtonClickListener.onSuccess(editText.getText().toString());
}
}
});
addView(view);
}
setButtonClickListener setButtonClickListener;
public void setOnButtonClickListener(setButtonClickListener buttonClickListener){
this.setButtonClickListener=buttonClickListener;
}
public interface setButtonClickListener{
void onSuccess(String str);
}
添加纪录
private Context mContext;
int mChildenerHeight;
int mHeight=10;
int mWidth=10;
public CustomFlawView(Context context) {
super(context);
mContext=context;
}
public CustomFlawView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mContext=context;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获取父窗口的
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int HeightSize = MeasureSpec.getSize(heightMeasureSpec);
measureChildren(widthMeasureSpec,heightMeasureSpec);
findData();
int left=0,top=0;
int childCount = getChildCount();
for (int i=0;i<childCount;i++){
View childAt = getChildAt(i);
if(left!=0){
if((left+childAt.getMeasuredWidth()>widthSize)){
top+=mChildenerHeight+mHeight;
left = 0;
}
}
left+=childAt.getMeasuredWidth()+mWidth;
}
setMeasuredDimension(widthSize,(top+mChildenerHeight)>HeightSize?HeightSize:top+mChildenerHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
findData();
int top=0,left=0;
int childCount = getChildCount();
for (int i=0;i<childCount;i++){
View childAt = getChildAt(i);
if(left!=0){
if((left+childAt.getMeasuredWidth()>getWidth())){
top+=mChildenerHeight+mHeight;
left=0;
}
}
childAt.layout(left,top,left+childAt.getMeasuredWidth(),top+childAt.getMeasuredHeight());
left+=childAt.getMeasuredWidth()+mWidth;
}
}
private void findData() {
mChildenerHeight=0;
int childCount = getChildCount();
for (int i=0;i<childCount;i++){
View childAt = getChildAt(i);
if(childAt.getMeasuredHeight()>mChildenerHeight){
mChildenerHeight=childAt.getMeasuredHeight();
}
}
}
数据库
public MySQLiteOpenHelper(Context context) {
super(context, "User.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table users(id integer primary key autoincrement," +
"_id text," +
"num text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
Userdao
private MySQLiteOpenHelper sqLiteOpenHelper;
private SQLiteDatabase database;
public UserDao(Context context){
sqLiteOpenHelper=new MySQLiteOpenHelper(context);
database=sqLiteOpenHelper.getReadableDatabase();
}
public void add(String _id,String num){
ContentValues values = new ContentValues();
values.put("num",num);
values.put("_id",_id);
database.insert("users",null,values);
}
public void delAll(){
database.delete("users",null,null);
}
public void del(String _id){
database.delete("users","_id=?",new String[]{_id});
}
public List<UserBean> select(){
ArrayList<UserBean> list = new ArrayList<>();
Cursor cursor = database.query("users", null, null, null, null, null, null);
while (cursor.moveToNext()){
String num = cursor.getString(cursor.getColumnIndex("num"));
String _id = cursor.getString(cursor.getColumnIndex("_id"));
UserBean userBean=new UserBean(num,_id);
list.add(userBean);
}
return list;
}
edit_bg
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1px"/>
<corners android:radius="4dp"/>
<solid android:color="#fff"/>
</shape>
tiitle布局
<ImageView
android:id="@+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/search"
/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入字体"
/>