DBManger.java
package com.example.famliyaccountbook; import android.content.*; import android.database.sqlite.SQLiteDatabase; import android.database.Cursor; import android.widget.*; public class DBManger { private Context context; private static DBManger instance; private String result = ""; private DBHelper helper; // 操作表的对象,进行增删改查 private SQLiteDatabase writableDatabase; public DBManger(Context context) { this.context = context; DBHelper dbHelper = new DBHelper(context, 1); writableDatabase = dbHelper.getWritableDatabase(); } public static DBManger getInstance(Context context) { if (instance == null) { synchronized (DBManger.class) { if (instance == null) { instance = new DBManger(context); } } } return instance; } //增加数据 public void add(String date, String type, String money) { ContentValues contentValues = new ContentValues(); contentValues.put("date", date); contentValues.put("type", type); contentValues.put("money", money); writableDatabase.insert("bill", null, contentValues); writableDatabase.close(); } //根据金额修改数据 public void update(String money) { ContentValues values = new ContentValues(); values.put("money", money); writableDatabase.update("bill", values, "money=?", new String[]{money}); writableDatabase.close(); } //根据日期删除数据 public void delete(String date) { writableDatabase.delete("bill", "date=?", new String[]{date}); writableDatabase.close(); } }
功能主页面
MainActivity
package com.example.famliyaccountbook; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button button_write; private Button button_list; private Button button_input; private Button button_finish; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); button_write = findViewById(R.id.button_write); button_write.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, WriteActivity.class); finish(); startActivity(intent); } }); button_list= findViewById(R.id.button_list); button_list.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ListActivity.class); finish(); startActivity(intent); } }); button_finish=findViewById(R.id.button_finish); button_finish.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }
activity_main.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" android:background="@drawable/main_activity" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="171dp" android:layout_marginLeft="171dp" android:layout_marginTop="131dp" android:layout_marginEnd="170dp" android:layout_marginRight="170dp" android:layout_marginBottom="76dp" android:text="家庭小账本" app:layout_constraintBottom_toTopOf="@+id/button_write" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button_write" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="154dp" android:layout_marginLeft="154dp" android:layout_marginTop="76dp" android:layout_marginEnd="153dp" android:layout_marginRight="153dp" android:layout_marginBottom="40dp" android:text="记账" app:layout_constraintBottom_toTopOf="@+id/button_list" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> <Button android:id="@+id/button_list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="161dp" android:layout_marginLeft="161dp" android:layout_marginTop="2dp" android:layout_marginEnd="162dp" android:layout_marginRight="162dp" android:layout_marginBottom="39dp" android:text="查看账本" app:layout_constraintBottom_toTopOf="@+id/button_finish" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button_write" /> <Button android:id="@+id/button_finish" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="161dp" android:layout_marginLeft="161dp" android:layout_marginEnd="162dp" android:layout_marginRight="162dp" android:layout_marginBottom="243dp" android:text="退出系统" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button_list" /> </androidx.constraintlayout.widget.ConstraintLayout>