<?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=".MainActivity">
<com.example.weeklx01.TitleBar
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<com.example.weeklx01.Kojian
android:id="@+id/lishi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索历史"
/>
<com.example.weeklx01.MyView
android:id="@+id/myview"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<com.example.weeklx01.Kojian
android:id="@+id/remen"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="热门搜索"
/>
<com.example.weeklx01.MyView
android:id="@+id/myview1"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:background="@drawable/search"
android:layout_width="50dp"
android:layout_height="50dp" />
<EditText
android:id="@+id/edit"
android:layout_width="330dp"
android:layout_height="wrap_content" />
</LinearLayout>
//Title接口
public class TitleBar extends LinearLayout {
private ImageView imageView;
private EditText editText;
Context context;
public TitleBar(Context context) {
super(context);
this.context=context;
init();
}
public TitleBar(Context context,AttributeSet attrs) {
super(context, attrs);
this.context=context;
init();
}
private void init() {
View view=View.inflate(context,R.layout.activity_titlebar,null);
imageView=view.findViewById(R.id.image);
editText=view.findViewById(R.id.edit);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (msetButton!=null){
msetButton.setsuccess(editText.getText().toString());
}
}
});
addView(view);
}
SetButton msetButton;
public void setBut(SetButton setButton){
msetButton=setButton;
}
public interface SetButton{
void setsuccess(String str);
}
}
//MAIN
public class MainActivity extends AppCompatActivity {
private MyView myView,myview1;
private TitleBar titleBar;
private Dao dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inif();
}
private void inif() {
//获取资源id
myView=findViewById(R.id.myview);
titleBar=findViewById(R.id.title);
myview1=findViewById(R.id.myview1);
//实例化dao
dao=new Dao(MainActivity.this);
//调用dao查询
final List<Bean> select = dao.select();
for (int i = 0; i < select.size(); i++) {
TextView text=new TextView(MainActivity.this);
//进来查询
final int index = i;
String name = select.get(i).getName();
text.setTextColor(Color.YELLOW);
text.setBackgroundResource(R.drawable.deit);
text.setText(name);
myView.addView(text);
//点击删除
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao.delete(select.get(index).getUuid());
myView.removeView(v);
}
});
}
//调用接口方法
titleBar.setBut(new TitleBar.SetButton() {
@Override
public void setsuccess(String str) {
//用uuid删除
final UUID uuid = UUID.randomUUID();
TextView tv=new TextView(MainActivity.this);
tv.setTextColor(Color.GREEN);
tv.setText(str);
tv.setBackgroundResource(R.drawable.deit);
tv.setTag(uuid);
myView.addView(tv);
//数据库添加
dao.add(str,uuid.toString());
//点击删除
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uuid = String.valueOf(v.getTag());
dao.delete(uuid);
myView.removeView(v);
}
});
}
});
//热门搜索
for (int i = 0; i < 20; i++) {
TextView textView=new TextView(MainActivity.this);
textView.setText("数量"+i);
textView.setTextColor(Color.RED);
textView.setBackgroundResource(R.drawable.deit);
myview1.addView(textView);
}
}
}
//控件
@SuppressLint("AppCompatCustomView")
public class Kojian extends TextView {
public Kojian(Context context) {
super(context);
}
public Kojian(Context context,AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Kojian);
int color = typedArray.getColor(R.styleable.Kojian_TextColor, Color.RED);
setTextColor(color);
//回收
typedArray.recycle();
}
}
<resources>
<declare-styleable name="Kojian">
<attr name="TextColor" format="color"></attr>
<attr name="TextName" format="string"></attr>
</declare-styleable>
</resources>
//
public class MyView extends LinearLayout {
//最高的孩子
private int mMax=20;
//间隔
private int mJj=20;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);
measureChildren(widthMeasureSpec,heightMeasureSpec);
findH();
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())>w){
top+=mMax+mJj;
left=0;
}
}
left+=view.getMeasuredWidth()+mJj;
}
setMeasuredDimension(w,(top+mMax)>h?h:top+mMax);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
findH();
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+=mMax+mJj;
left=0;
}
}
view.layout(left,top,left+view.getMeasuredWidth(),top+mMax);
left+=view.getMeasuredWidth()+mJj;
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
private void findH() {
mMax=0;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
if (view.getMeasuredHeight()>mMax){
mMax=view.getMeasuredHeight();
}
}
}
}
//数据库
public class Dao {
MySqilte mySqilte;
SQLiteDatabase database;
public Dao(Context context){
mySqilte=new MySqilte(context);
database=mySqilte.getReadableDatabase();
}
public void add(String name,String uuid){
ContentValues values=new ContentValues();
values.put("name",name);
values.put("uuid",uuid);
database.insert("user",null,values);
}
public List<Bean> select(){
List<Bean> list=new ArrayList<>();
Cursor query = database.query("user", null, null, null, null, null, null);
while (query.moveToNext()){
String name = query.getString(query.getColumnIndex("name"));
String uuid = query.getString(query.getColumnIndex("uuid"));
Bean bean=new Bean(name,uuid);
list.add(bean);
}
return list;
}
public void delete(String uuid){
database.delete("user","uuid=?",new String[]{uuid});
}
}