头部输入框和按钮自定义View
package jx.com.day4_waterfall3.view;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import jx.com.day4_waterfall3.R;
public class HeadLayout extends LinearLayout {
private Context mcontext;
public HeadLayout(Context context) {
super(context);
init();
}
public HeadLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mcontext = context;
init();
}
private void init(){
View view = inflate(mcontext, R.layout.headview,null);
final EditText editText = view.findViewById(R.id.edit);
ImageView search = view.findViewById(R.id.search);
search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString();
callBack.onButtonClick(s);
}
});
addView(view);
}
public CallBack callBack;
public void setOnButtonClisk(CallBack callback){
callBack = callback;
}
public interface CallBack{
void onButtonClick(String name);
}
}
标签名(自定义属性)
package jx.com.day4_waterfall3.view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;
import jx.com.day4_waterfall3.R;
@SuppressLint("AppCompatCustomView")
public class Sousuolishi extends TextView {
private Context mcontext;
public Sousuolishi(Context context) {
super(context);
}
public Sousuolishi(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mcontext = context;
//自定义属性
//第一步,在value文件夹下建立一个attr.xml文件,
//第二步,写<declear....标签
//第三步,写<attr 标签 name:方法名 format:属性
//第四步,在布局文件根控件中写xmlns:app="http://schemas.android.com/apk/res-auto"
//第五步,在想要调用自定义属性的控件中添加app:方法名=“想要设置的值”
//第六步,在自定义view中的有AttributeSet的构造方法里写以下代码
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FlowLayout);
int color = typedArray.getColor(R.styleable.FlowLayout_textcolor, Color.BLUE);
setTextColor(color);
typedArray.recycle();
}
}
输入标签的自定义View
package jx.com.day4_waterfall3.view;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
public class FlowLayout extends LinearLayout {
private int mMaxChildHeigh;
private int marginleft = 20;
private int margintop = 20;
public FlowLayout(Context context) {
super(context);
}
public FlowLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//父控件的宽高
int widthsize = MeasureSpec.getSize(widthMeasureSpec);
int heighsize = MeasureSpec.getSize(heightMeasureSpec);
//孩子的大小
measureChildren(widthMeasureSpec,heightMeasureSpec);
//查找最高的孩子
findMaxHeight();
//初始化
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())>widthsize){
//换行,加上边距
top+=mMaxChildHeigh+margintop;
left = 0;
}
}
//孩子的宽度加上编剧
left+=view.getMeasuredWidth()+marginleft;
}
//给自定义View设置高度
setMeasuredDimension(widthsize,(top+mMaxChildHeigh)>heighsize?heighsize:top+mMaxChildHeigh);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
//找到最高的孩子
findMaxHeight();
int left = 0,top = 0;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
if(left != 0){
if((view.getMeasuredWidth()+left)>getWidth()){
top+=mMaxChildHeigh+margintop;
left = 0;
}
}
//为每个孩子设置在父控件的位置
view.layout(left,top,left+view.getMeasuredWidth(),top+mMaxChildHeigh);
left+=view.getMeasuredWidth()+marginleft;
}
}
//遍历找到最高的孩子
private void findMaxHeight(){
mMaxChildHeigh = 0;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
if(view.getMeasuredHeight()>mMaxChildHeigh){
mMaxChildHeigh = view.getMeasuredHeight();
}
}
}
}
MainAcitivity
package jx.com.day4_waterfall3;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import java.util.UUID;
import jx.com.day4_waterfall3.bean.NameBean;
import jx.com.day4_waterfall3.sqlite.NameDao;
import jx.com.day4_waterfall3.view.FlowLayout;
import jx.com.day4_waterfall3.view.HeadLayout;
public class MainActivity extends AppCompatActivity {
private HeadLayout headLayout;
private FlowLayout flowLayout,remen;
private NameDao dao;
private ImageView lajitong;
private String[] contents = new String[]{"美 女", "女 神", "热 舞", "丰 满", "性 感", "知 性"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//实例化
dao = new NameDao(MainActivity.this);
setContentView(R.layout.activity_main);
lajitong = findViewById(R.id.lajitong);
headLayout = findViewById(R.id.headview);
flowLayout = findViewById(R.id.flowview);
remen = findViewById(R.id.flowviewremen);
//查找
final List<NameBean> select = dao.select();
for (int i = 0; i < select.size(); i++) {
final int index = i;
TextView textView = new TextView(MainActivity.this);
textView.setText(select.get(i).getName());
textView.setTextColor(Color.RED);
textView.setBackgroundResource(R.drawable.myshape);
textView.setPadding(10,10,10,10);
textView.setTextSize(18);
flowLayout.addView(textView);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao.del(select.get(index).getRandom());
flowLayout.removeView(v);
}
});
}
lajitong.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao.delall();
flowLayout.removeAllViews();
}
});
headLayout.setOnButtonClisk(new HeadLayout.CallBack() {
@Override
public void onButtonClick(String name) {
//随机字符串
String randomnum = UUID.randomUUID().toString();
TextView textView = new TextView(MainActivity.this);
textView.setTag(randomnum);
textView.setText(name);
textView.setTextColor(Color.RED);
//加入数据库
dao.add(name,randomnum);
textView.setBackgroundResource(R.drawable.myshape);
textView.setPadding(10,10,10,10);
textView.setTextSize(18);
//加入flowLayout布局
flowLayout.addView(textView);
//点击删除
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = String.valueOf(v.getTag());
dao.del(s);
//删除
flowLayout.removeView(v);
}
});
}
});
for (int i = 0; i < contents.length; i++) {
TextView textView = new TextView(MainActivity.this);
textView.setText(contents[i]);
textView.setTextColor(Color.RED);
textView.setBackgroundResource(R.drawable.myshape);
textView.setPadding(10,10,10,10);
textView.setTextSize(18);
remen.addView(textView);
}
}
}
MianActivityXml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<jx.com.day4_waterfall3.view.HeadLayout
android:id="@+id/headview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></jx.com.day4_waterfall3.view.HeadLayout>
<RelativeLayout
android:id="@+id/ppp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/headview"
>
<jx.com.day4_waterfall3.view.Sousuolishi
android:id="@+id/lishi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索历史"
android:textSize="25sp"
app:textcolor="#f00"
/>
<ImageView
android:id="@+id/lajitong"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:background="@drawable/laji"
/>
</RelativeLayout>
<jx.com.day4_waterfall3.view.FlowLayout
android:id="@+id/flowview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/ppp"
android:layout_marginTop="20dp"
></jx.com.day4_waterfall3.view.FlowLayout>
<jx.com.day4_waterfall3.view.Sousuolishi
android:id="@+id/remen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="热门搜索"
android:textSize="25sp"
app:layout_constraintTop_toBottomOf="@+id/flowview"
app:textcolor="#f00"
/>
<jx.com.day4_waterfall3.view.FlowLayout
android:id="@+id/flowviewremen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/remen"
android:layout_marginTop="20dp"
></jx.com.day4_waterfall3.view.FlowLayout>
</android.support.constraint.ConstraintLayout>
创建数据库库
package jx.com.day4_waterfall3.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
public class MySqliteHelper extends SQLiteOpenHelper {
public MySqliteHelper(@Nullable Context context) {
super(context, "data_db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table data(id integer primary key autoincrement," +
"name text," +
"randomnum text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Dao
package jx.com.day4_waterfall3.sqlite;
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;
import jx.com.day4_waterfall3.bean.NameBean;
public class NameDao {
private SQLiteDatabase base;
public NameDao (Context context){
MySqliteHelper mySqliteHelper = new MySqliteHelper(context);
base = mySqliteHelper.getReadableDatabase();
}
public void add(String name,String randomnum){
ContentValues values = new ContentValues();
values.put("name",name);
values.put("randomnum",randomnum);
base.insert("data",null,values);
}
public List<NameBean> select(){
List<NameBean> list = new ArrayList<>();
Cursor query = base.query("data", null, null, null, null, null, null);
while (query.moveToNext()){
String name = query.getString(query.getColumnIndex("name"));
String randomnum = query.getString(query.getColumnIndex("randomnum"));
NameBean nameBean = new NameBean(name, randomnum);
list.add(nameBean);
}
return list;
}
public void del(String randomnum){
base.delete("data","randomnum=?",new String[]{randomnum});
}
public void delall(){
base.delete("data",null,null);
}
}