简介
用Sqlite做数据库,用来存储账号以及笔记信息,实现了账号的注册以及登录功能,笔记方面实现了新增、删除、修改、搜索功能,列表展示笔记使用的是listView(懒得弄分割线,就使用listView的默认分割线了);
运行效果
代码讲解
我代码里使用了两个依赖,一个是工具类,一个是标题栏,工具类是使用里面的SPUtils保存当前登录的账号信息,不用再次输入账号密码,可按需引入
//标题栏
implementation 'com.github.FlyJingFish:TitleBar:1.2.5'
//工具类
implementation 'com.blankj:utilcodex:1.31.1'
首先是数据库的实现部分,新建一个DBOpenHelper 类,继承SQLiteOpenHelper
public class DBOpenHelper extends SQLiteOpenHelper {
private DBOpenHelper dbOpenHelper;
private SQLiteDatabase db;
private static final String DBNAME="notes.db";
private static final int VERSION=1;
public DBOpenHelper(Context context) {
super(context, DBNAME, null, VERSION);
dbOpenHelper=this;
db=dbOpenHelper.getWritableDatabase();
}
//创建数据库
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据表
db.execSQL("create table if not exists user(id INTEGER primary key autoincrement,username varchar(25),password varchar(20))");
db.execSQL("create table if not exists notes(id INTEGER primary key autoincrement,title varchar(20),content varchar(255),time varchar(20)," +
"username varchar(20))");
}
//升级数据库
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
//插入用户数据
public boolean insertUserData(String username,String password){
ContentValues contentValues=new ContentValues();
contentValues.put("username",username);
contentValues.put("password",password);
return db.insert("user",null,contentValues)>0;
}
public boolean insertNotes(String title,String content,String time,String username){
ContentValues contentValues=new ContentValues();
contentValues.put("title",title);
contentValues.put("content",content);
contentValues.put("time",time);
contentValues.put("username",username);
return db.insert("notes",null,contentValues)>0;
}
public boolean updateNotes(String id,String title,String content,String time){
ContentValues contentValues=new ContentValues();
contentValues.put("title",title);
contentValues.put("content",content);
contentValues.put("time",time);
String sql="id=?";
String[] strings=new String[]{id};
return db.update("notes",contentValues,sql,strings)>0;
}
public boolean deleteNotes(String id){
String sql="id=?";
String[] contentValuesArray=new String[]{id};
return db.delete("notes",sql,contentValuesArray)>0;
}
//获取笔记
public List<Notes> getNotes(String query){
List<Notes> list=new ArrayList<>();
Cursor cursor;
if (query==null){
cursor=db.rawQuery("select * from notes where username =?",new String[]{SPUtils.getInstance().getString("username")});
}
else {
cursor=db.rawQuery("select * from notes where username =? and title like ?",new String[]{SPUtils.getInstance().getString("username"),"%"+query+"%"});
}
if (cursor!=null){
while (cursor.moveToNext()){
@SuppressLint("Range") int id=cursor.getInt(cursor.getColumnIndex("id"));
@SuppressLint("Range") String title=cursor.getString(cursor.getColumnIndex("title"));
@SuppressLint("Range") String content=cursor.getString(cursor.getColumnIndex("content"));
@SuppressLint("Range") String time=cursor.getString(cursor.getColumnIndex("time"));
@SuppressLint("Range") String user=cursor.getString(cursor.getColumnIndex("username"));
Notes note=new Notes(id,title,content,time,user);
list.add(note);
}
cursor.close();
}
return list;
}
}
然后是基类Activity,这个是为了方便统一管理标题栏和数据库
创建一个BaseActivity,继承AppCompatActivity
public class BaseActivity extends AppCompatActivity {
protected TitleBar titleBar;
protected DBOpenHelper dbOpenHelper;
protected SQLiteDatabase db;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initTitle();
dbOpenHelper=new DBOpenHelper(this);
db=dbOpenHelper.getWritableDatabase();
}
public void initTitle(){
titleBar = new TitleBar(this);
titleBar.setShadow(1, Color.parseColor("#40454545"), TitleBar.ShadowType.GRADIENT);
titleBar.setTitleGravity(TitleBar.TitleGravity.CENTER);
titleBar.setOnBackViewClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
titleBar.setAboveContent(true);
titleBar.attachToWindow();
}
@Override
protected void onDestroy() {
super.onDestroy();
db.close();
dbOpenHelper.close();
}
}
然后就是笔记实体类
public class Notes implements Serializable {
private int id;
private String title;
private String content;
private String time;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Notes() {
}
public Notes(int id, String title, String content, String time,String user) {
this.id = id;
this.title = title;
this.content = content;
this.time = time;
}
@Override
public String toString() {
return "Notes{" +
"id=" + id +
", title='" + title + ''' +
", content='" + content + ''' +
", time='" + time + ''' +
", username='" + username + ''' +
'}';
}
}
登录注册页面
登录页面
public class Login extends BaseActivity {
private EditText username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
titleBar.setTitle("登录");
titleBar.setDisplayLeftView(false);
username=findViewById(R.id.et_user_name);
password=findViewById(R.id.et_psw);
String name=SPUtils.getInstance().getString("username");
String pw=SPUtils.getInstance().getString("password");
if (!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(pw)){
username.setText(name);
password.setText(pw);
}
//点击登录
findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
login();
}
});
//去注册
findViewById(R.id.btn_register).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(Login.this, Register.class);
intent.putExtra("flag","login");
startActivity(intent);
}
});
}
public void login(){
String name =username.getText().toString();
String pw =password.getText().toString();
if (name.equals("")||pw.equals("")){
Toast.makeText(Login.this,"请输入账号密码!",Toast.LENGTH_SHORT).show();
}
else {
//查询用户
@SuppressLint("Recycle") Cursor cursor=db.rawQuery("select * from user where username=?",new String[]{name});
cursor.moveToFirst();
if (cursor.getCount()==0){
Toast.makeText(Login.this,"不存在该用户!",Toast.LENGTH_SHORT).show();
}
else{
@SuppressLint("Range") String password=cursor.getString(cursor.getColumnIndex("password"));
if (pw.equals(password)){
Toast.makeText(Login.this,"登录成功!",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
//保存用户名到SharedPreferences
SPUtils.getInstance().put("username",name);
SPUtils.getInstance().put("password",pw);
}else {
Toast.makeText(Login.this,"密码错误!",Toast.LENGTH_SHORT).show();
}
}
}
}
}
<?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"
android:gravity="center"
tools:context=".ui.Login">
<EditText
android:id="@+id/et_user_name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/edittext_style"
android:gravity="center_vertical"
android:hint="请输入用户名"
android:paddingLeft="15dp"
android:layout_marginHorizontal="20dp"
android:singleLine="true"
android:textColor="@color/black"
android:textSize="14sp" />
<!--输入框-->
<EditText
android:id="@+id/et_psw"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/edittext_style"
android:gravity="center_vertical"
android:hint="请输入密码"
android:paddingLeft="15dp"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="10dp"
android:inputType="textPassword"
android:singleLine="true"
android:textColor="@color/black"
android:textSize="14sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_login"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:background="@drawable/button_style"
android:text="登 录"
android:textColor="@android:color/white"
android:textSize="18sp" />
<Button
android:id="@+id/btn_register"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="15dp"
android:layout_marginRight="35dp"
android:background="@drawable/button_style"
android:text="注册"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
public class Register extends BaseActivity {
private EditText username,password,pw_again;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
titleBar.setTitle("欢迎注册");
username=findViewById(R.id.et_user_name);
password=findViewById(R.id.et_psw);
pw_again=findViewById(R.id.et_psw_again);
//注册
findViewById(R.id.btn_register).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
register();
}
});
}
public void register(){
String name =username.getText().toString();
String pw =password.getText().toString();
String pwa =pw_again.getText().toString();
if (name.equals("")||pw.equals("")||pwa.equals("")){
Toast.makeText(Register.this,"请输入完整!",Toast.LENGTH_SHORT).show();
}
else {
//查询用户
Cursor cursor = db.rawQuery("select * from user where username=?", new String[]{name});
cursor.moveToFirst();
if (cursor.getCount() == 0) {
if (!pw.equals(pwa)) {
Toast.makeText(Register.this, "两次密码不相同!", Toast.LENGTH_SHORT).show();
} else {
if (dbOpenHelper.insertUserData(name, pw)){
Toast.makeText(Register.this, "注册成功!", Toast.LENGTH_SHORT).show();
finish();
}
}
} else {
Toast.makeText(Register.this, "该用户已存在!", Toast.LENGTH_SHORT).show();
}
}
}
}
首页
public class MainActivity extends BaseActivity {
private List<Notes> noteList=new ArrayList<>();
private ListView listView;
private NoteAdapter noteAdapter;
private SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleBar.setTitle("我的记事本");
titleBar.getRightTextView().setText("新增");
titleBar.setOnRightViewClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, AddNote.class));
}
});
listView=findViewById(R.id.listView);
searchView=findViewById(R.id.searchView);
//初始化适配器
noteAdapter=new NoteAdapter(this,R.layout.note_item, noteList);
//设置适配器
listView.setAdapter(noteAdapter);
//搜索笔记
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (!TextUtils.isEmpty(query)){
noteList.clear();
noteList.addAll(dbOpenHelper.getNotes(query));
noteAdapter.notifyDataSetChanged();
searchView.clearFocus();
}
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)){
noteList.clear();
noteList.addAll(dbOpenHelper.getNotes(null));
noteAdapter.notifyDataSetChanged();
searchView.clearFocus();
}
return false;
}
});
}
@Override
protected void onResume() {
super.onResume();
noteList.clear();
noteList.addAll(dbOpenHelper.getNotes(null));
noteAdapter.notifyDataSetChanged();
}
}
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<androidx.appcompat.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:iconifiedByDefault="false"
android:background="@drawable/edittext_style"
app:queryHint="输入内容进行搜索"
android:layout_marginHorizontal="20dp"/>
<ListView
android:id="@+id/listView"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
最后一个就是新增笔记页面了
由于新增和修改差不多,所以都放在一个页面了,根据flag来判断是新增还是修改
public class AddNote extends BaseActivity {
private EditText title,content;
private TextView number,time;
private String flag;
private Notes note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_note);
title=findViewById(R.id.title);
content=findViewById(R.id.content);
number=findViewById(R.id.number);
time=findViewById(R.id.time);
flag = getIntent().getStringExtra("flag");
titleBar.getRightTextView().setText("保存");
if (flag!=null){
note = (Notes) getIntent().getSerializableExtra("entity");
title.setText(note.getTitle());
content.setText(note.getContent());
time.setText("上次修改时间:"+ note.getTime());
number.setText(note.getContent().length()+"字");
titleBar.setTitle("修改记事本");
}
//设置时间
else {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss", Locale.CHINA);
time.setText(simpleDateFormat.format(date));
titleBar.setTitle("新增记事本");
}
//更新字数
content.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
number.setText(content.getText().toString().length()+"字");
}
@Override
public void afterTextChanged(Editable editable) {
}
});
titleBar.setOnRightViewClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss", Locale.CHINA);
if (!"".equals(title.getText().toString())&&!"".equals(content.getText().toString())){
if (flag==null){
if (dbOpenHelper.insertNotes(title.getText().toString(),content.getText().toString(),simpleDateFormat.format(date), SPUtils.getInstance().getString("username"))){
Toast.makeText(AddNote.this, "保存成功!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(AddNote.this, "未知错误", Toast.LENGTH_SHORT).show();
}
}
else {
if (dbOpenHelper.updateNotes(String.valueOf(note.getId()),title.getText().toString(),content.getText().toString(),simpleDateFormat.format(date))){
Toast.makeText(AddNote.this, "修改成功!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(AddNote.this, "未知错误", Toast.LENGTH_SHORT).show();
}
}
finish();
}
else {
Toast.makeText(AddNote.this, "请输入内容!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".ui.AddNote">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.065"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0字"
android:textSize="16sp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.077"
app:layout_constraintStart_toEndOf="@+id/time"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="标题"
android:inputType="text"
android:lines="1"
android:maxLength="10"
android:paddingLeft="10dp"
android:textColor="@color/black"
android:textSize="30sp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/number"
android:autofillHints="" />
<EditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:hint="内容"
android:background="@null"
android:gravity="start"
android:textSize="25sp"
android:paddingStart="10dp"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title"
android:autofillHints="" />
</androidx.constraintlayout.widget.ConstraintLayout>
最后就是listView的适配器了
//listview适配器
public class NoteAdapter extends ArrayAdapter<Notes> {
private Context context;
private DBOpenHelper dbOpenHelper;
//构造方法
public NoteAdapter(@NonNull Context context, int resource, List<Notes> diaryList) {
super(context, resource,diaryList);
this.context=context;
dbOpenHelper=new DBOpenHelper(context);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Notes note=getItem(position);
@SuppressLint("ViewHolder") View view= LayoutInflater.from(getContext()).inflate(R.layout.note_item,parent,false);
TextView title = view.findViewById(R.id.title);
TextView create_time = view.findViewById(R.id.create_time);
TextView content=view.findViewById(R.id.content);
ImageView del = view.findViewById(R.id.del);
LinearLayout constraintLayout=view.findViewById(R.id.con);
//设置item内容
title.setText(note.getTitle());
content.setText(note.getContent());
create_time.setText(note.getTime());
//删除item
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dbOpenHelper.deleteNotes(String.valueOf(note.getId()))){
Toast.makeText(context,"已删除",Toast.LENGTH_SHORT).show();
remove(note);
notifyDataSetChanged();
}
else {
Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
}
}
});
//单击item
constraintLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(context, AddNote.class);
//设置flag
intent.putExtra("flag","update");
//将笔记内容传递过去
intent.putExtra("entity",note);
context.startActivity(intent);
}
});
return view;
}
}
源码
github:https://github.com/panzhusheng/Notes
gitee:https://gitee.com/pan-zs/notes
原文地址:https://blog.csdn.net/butterfly_new/article/details/132182595
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_23414.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。