效果图
依赖;Project:
repositories { google() jcenter() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:3.1.2' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }
app:
apply plugin: 'com.android.application' apply plugin: 'org.greenrobot.greendao' android { compileSdkVersion 27 defaultConfig { applicationId "com.wode.date0522" minSdkVersion 15 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' compile 'org.greenrobot:greendao:3.2.2' // add library compile 'org.greenrobot:greendao-generator:3.2.2' //(GreenDao依赖) compile 'com.fynn.fluidlayout:fluidlayout:1.0' }
Bean类:
import org.greenrobot.greendao.annotation.Entity; import org.greenrobot.greendao.annotation.Id; import org.greenrobot.greendao.annotation.Unique; import org.greenrobot.greendao.annotation.Generated; @Entity public class Bean { @Id(autoincrement = true) private Long id; @Unique private String name; @Generated(hash = 417670818) public Bean(Long id, String name) { this.id = id; this.name = name; } @Generated(hash = 80546095) public Bean() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } }
自定义MyTitleview:
import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RelativeLayout; public class MyTitleview extends RelativeLayout { private EditText editText; private Button btn_sousuo; public MyTitleview(Context context) { this(context,null); } public MyTitleview(Context context, AttributeSet attrs) { this(context, attrs,0); } public MyTitleview(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context,attrs); } private void init(Context context, AttributeSet attrs) { View inflate = View.inflate(context, R.layout.shousuo, this); editText = inflate.findViewById(R.id.sousuo_ed); btn_sousuo = inflate.findViewById(R.id.btn_sousuo); btn_sousuo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { listened.toString(editText.getText().toString()); editText.setText(""); } }); } Listened listened; public void setListened(Listened listened){ this.listened=listened; } public interface Listened{ void toString(String editText); } }
shousuo布局:
<?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="60dp" android:background="#fff"> <View android:id="@+id/v" android:layout_width="50dp" android:layout_height="1dp" /> <EditText android:id="@+id/sousuo_ed" android:layout_width="200dp" android:layout_height="40dp" android:layout_margin="10dp" android:hint=" 请输入搜索内容" android:layout_toRightOf="@id/v" android:background="@drawable/edittext_shape" /> <Button android:id="@+id/btn_sousuo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/sousuo_ed" android:layout_centerVertical="true" android:text="搜索" /> </RelativeLayout>
drawable/edittext_shape:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="100dp"/> <solid android:color="#fff"/> <stroke android:color="#f0f2f5" android:width="1dp"/> </shape>
MainActivity 主类:
import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.fynn.fluidlayout.FluidLayout; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private BeanDao beanDao; private Button btn_all; private MyTitleview main_mytitle; private FluidLayout fluidLayout; private List<Bean> stringList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 通过 DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的 SQLiteOpenHelper 对象。 // 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为 greenDAO 已经帮你做了。 // 注意:默认的 DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。 // 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。 DaoMaster.DevOpenHelper mHelper = new DaoMaster.DevOpenHelper (this, "sport-db", null); SQLiteDatabase db = mHelper.getWritableDatabase(); // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。 DaoMaster mDaoMaster = new DaoMaster(db); DaoSession daoSession = mDaoMaster.newSession(); beanDao = daoSession.getBeanDao(); //找到控件 btn_all = findViewById(R.id.btn_all); main_mytitle = findViewById(R.id.main_mytitle); fluidLayout = findViewById(R.id.fluidLayout); main_mytitle.setListened(new MyTitleview.Listened() { @Override public void toString(String editText) { if(!TextUtils.isEmpty(editText)){ //存入数据库 beanDao.insertOrReplaceInTx(new Bean(null,editText)); genTag(); }else{ Toast.makeText(MainActivity.this, "不能为空", Toast.LENGTH_SHORT).show(); } } }); //清空按钮 btn_all.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //删除数据库中的所有数据 beanDao.deleteAll(); //查询数据库 genTag(); } }); genTag(); } private void genTag() { List<Bean> beans = beanDao.loadAll(); stringList.clear(); stringList.addAll(beans); fluidLayout.removeAllViews(); for (int x=0;x<beans.size();x++){ TextView tv = new TextView(MainActivity.this); tv.setText(stringList.get(x).getName()); tv.setTextSize(13); FluidLayout.LayoutParams params = new FluidLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.setMargins(12,12,12,12); fluidLayout.addView(tv,params); } } }
主类布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.wode.date0522.MyTitleview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/main_mytitle"> </com.wode.date0522.MyTitleview> <com.fynn.fluidlayout.FluidLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/fluidLayout"> </com.fynn.fluidlayout.FluidLayout> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:id="@+id/btn_all" android:text="清除历史记录"/> </LinearLayout>