1.记账本的启动界面
LauncherActivity文件:
1 package com.example.application_keep; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.os.Handler; 9 10 public class LauncherActivity extends Activity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_launcher); 16 new Handler().postDelayed(new Runnable() { 17 @Override 18 public void run() { 19 //在主线程中执行 20 startMainActivity(); 21 } 22 },2000); 23 } 24 //启动主页面 25 private void startMainActivity(){ 26 Intent intent=new Intent(this,MainActivity.class); 27 startActivity(intent); 28 //关闭当前页面 29 finish(); 30 } 31 }
activity_launcher.xml文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:background="@android:color/white" 8 tools:context=".LauncherActivity"> 9 10 <ImageView 11 android:id="@+id/la_icon" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:src="@drawable/ks" 15 16 android:layout_centerInParent="true" 17 /> 18 19 <TextView 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:layout_below="@id/la_icon" 23 android:layout_marginTop="-48dp" 24 android:text="家庭记账本" 25 android:layout_centerHorizontal="true" 26 android:textColor="@android:color/black" 27 android:textSize="35sp" /> 28 </RelativeLayout>
需要注意的是,要在AndroidManifest文件中将LauncherActivity设置为启动界面
效果:
启动APP时,出现启动界面,3秒后关闭
2.与数据库的连接
创建数据库
1.创建一个类继承SQLiteOpenHelper类
2.实现里面的方法,创建构造方法
1 public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 2 super(context, name, null, version); 3 mContext=context; 4 }
参数解释:
context:上下文
name:数据库名称,常定义为常量
factory:游标工厂
version:版本号
3.创建子类对象,再调用getReadableDatabase()/getWritableDatabase()方法
DatabaseHelper文件:
1 package com.example.keep;
2
3 import android.content.Context;
4 import android.database.sqlite.SQLiteDatabase;
5 import android.database.sqlite.SQLiteOpenHelper;
6 import android.util.Log;
7
8 import androidx.annotation.Nullable;
9 public class DatabaseHelper extends SQLiteOpenHelper {
10 private static final String TAG="DatabaseHelper";
11 public DatabaseHelper(@Nullable Context context) {
12 super(context, Constants.DATABASE_NAME, null, Constants.VERSION_CODE);
13 }
14
15 @Override
16 public void onCreate(SQLiteDatabase db) {
17 //创建时的回调
18 Log.d(TAG,"创建数据库...");
19 String sql="create table "+Constants.TABLE_NAME+" (id integer primary key autoincrement, in_out varchar,money float,date varchar,type varchar,way varchar)";
20 db.execSQL(sql);
21 }
22
23 @Override
24 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
25 //升级时的回调
26 Log.d(TAG,"升级数据库...");
27 }
28 }
constants文件:
1 public class Constants {
2 public static final String DATABASE_NAME="money_keep.db";
3 public static final int VERSION_CODE=1;
4 public static final String TABLE_NAME="KeepUser";
5 }
子类中使用:
3.主界面:
activity_main.xml文件:
<?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" android:background="@mipmap/bj_1"> <!--标题栏--> <include layout="@layout/titlebar" /> <TextView android:layout_width="wrap_content" android:layout_height="220dp"/> <Button android:id="@+id/bt_add" android:layout_width="110dp" android:layout_height="60dp" android:layout_gravity="center" android:background="#1E9AC0" android:text="记一笔" android:textColor="#FFFFFF" android:textSize="18dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/bt_show" android:layout_width="110dp" android:layout_height="60dp" android:layout_gravity="center" android:background="#1E9AC0" android:text="查看明细" android:textColor="#FFFFFF" android:textSize="18dp" /> </LinearLayout>
MainActivity文件:
1 package com.example.keep;
2
3 import androidx.appcompat.app.AppCompatActivity;
4
5 import android.content.Intent;
6 import android.os.Bundle;
7 import android.view.View;
8 import android.widget.Button;
9
10 public class MainActivity extends AppCompatActivity implements View.OnClickListener{
11 private Button button1;
12 private Button button2;
13 @Override
14 protected void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.activity_main);
17
18 button1=(Button)findViewById(R.id.bt_show) ;
19 button2 = (Button) findViewById(R.id.bt_add);
20 button1.setOnClickListener(this);
21 button2.setOnClickListener(this);
22 }
23
24 @Override
25 public void onClick(View v) {
26 switch (v.getId()){
27 case R.id.bt_show://执行按钮1
28 Intent intent =new Intent();
29 intent.setClass(getApplicationContext(), ShowActivity.class);
30 this.startActivity(intent);
31 break;
32 case R.id.bt_add:
33 Intent intent2 =new Intent();
34 intent2.setClass(getApplicationContext(), AddActivity.class);
35 this.startActivity(intent2);
36 break;
37
38 }
39 }
40 }
效果图:
4.记一笔功能实现
AddActivity文件代码:
package com.example.keep;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import static com.example.keep.Constants.TABLE_NAME;
public class AddActivity extends AppCompatActivity implements View.OnClickListener {
private DatabaseHelper myHelper;
private RadioButton ra_shouru;
private RadioButton ra_zhichu;
private EditText et_money;
private EditText et_date;
private EditText et_type;
private EditText et_way;
private TextView mTvShow;
private Button mBtAdd;
private Button mBtQuery;
private Button mBtUpdate;
private Button mBtDelete;
public String in="";
public String out="";
public String money;
public String date;
public String type;
public String way;
public SQLiteDatabase db;
public ContentValues values;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
myHelper = new DatabaseHelper(this);
init();
}
//实例化界面,获取各个控件的值
private void init() {
ra_shouru=(RadioButton) findViewById(R.id.shouru);
ra_zhichu=(RadioButton) findViewById(R.id.zhichu);
et_money=(EditText) findViewById(R.id.edit_money);
et_date=(EditText) findViewById(R.id.edit_date);
et_type=(EditText) findViewById(R.id.edit_type);
et_way=(EditText) findViewById(R.id.edit_way);
mTvShow = (TextView) findViewById(R.id.ld_show);
mBtAdd = (Button) findViewById(R.id.btn_add);
mBtUpdate = (Button) findViewById(R.id.btn_update);
mBtQuery = (Button) findViewById(R.id.btn_query);
mBtDelete = (Button) findViewById(R.id.btn_delete);
mBtAdd.setOnClickListener(this);
mBtUpdate.setOnClickListener(this);
mBtQuery.setOnClickListener(this);
mBtDelete.setOnClickListener(this);
}
@Override
//点击事件
public void onClick(View v) {
switch(v.getId()) {
//添加数据
case R.id.btn_add:
addText();
queryText();
break;
//查询数据
case R.id.btn_query:
queryText();
break;
//修改数据
case R.id.btn_update:
updateText();
queryText();
break;
//删除数据
case R.id.btn_delete:
deleteText();
queryText();
break;
}
}
//添加数据
public void addText() {
money = et_money.getText().toString(); //获取金额
date = et_date.getText().toString(); //获取时间
type=et_type.getText().toString(); //获取类别
way=et_way.getText().toString(); //获取方式
//获取可读写的SQLiteDatabase对象
db = myHelper.getWritableDatabase();
//创建ContentValues对象
values = new ContentValues();
//将数据添加到ContentValues对象
if(ra_shouru.isChecked()){
in="收入";
values.put("in_out",in);
}
if(ra_zhichu.isChecked()){
out="支出";
values.put("in_out",out);//存性别
}
values.put("money",money);
values.put("date",date);
values.put("type",type);
values.put("way",way);
if(in.equals("")&&out.equals("")||et_money.getText().toString().equals("")|| et_date.getText().toString().equals("")|| et_way.getText().toString().equals("")||et_type.getText().toString().equals(""))
{
Toast.makeText(AddActivity.this, "信息不全,请补充", Toast.LENGTH_SHORT).show();
}
else{
db.insert(TABLE_NAME,null,values);
//注意别漏掉
values.clear();
Toast.makeText(AddActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
et_money.setText("");//保存成功清空信息
et_date.setText("");
et_type.setText("");
et_way.setText("");
}
db.close();
}
//查询数据
public void queryText() {
db=myHelper.getWritableDatabase();
Cursor cursor = db.query(TABLE_NAME,null,null,null,null,null,null);
if(cursor.getCount() == 0) {
mTvShow.setText("");
Toast.makeText(this,"没有数据",Toast.LENGTH_SHORT).show();
}
else{
cursor.moveToFirst();
mTvShow.setText(cursor.getString(1) + "\t\t" + cursor.getString(2) + "元\t\t" + cursor.getString(3) +"\t "+cursor.getString(4)+"\t "+ cursor.getString(5));
}
while(cursor.moveToNext()) {
mTvShow.append("\n" + cursor.getString(1) + "\t\t" + cursor.getString(2) + "元\t\t" + cursor.getString(3) +"\t "+cursor.getString(4)+"\t "+ cursor.getString(5));
}
cursor.close();
db.close();
}
//修改数据
public void updateText() {
db=myHelper.getWritableDatabase();
//实例化一个要修改的数据的对象
values=new ContentValues();
values.put("date",date = et_date.getText().toString());
values.put("type",type = et_type.getText().toString());
values.put("way",way = et_way.getText().toString());
//更新并得到行数
db.update(TABLE_NAME,values,"money = ?",new String[]{et_money.getText().toString()});
Toast.makeText(this, "数据修改成功", Toast.LENGTH_SHORT).show();
db.close();
}
//删除数据
public void deleteText() {
SQLiteDatabase db=myHelper.getWritableDatabase();
int i = db.delete(TABLE_NAME,"money=?",new String[]{et_money.getText().toString()});
if(i !=0 ) {
Toast.makeText(this,"删除数据成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"删除数据失败",Toast.LENGTH_SHORT).show();
}
db.close();
}
}
activity_add.xml文件代码:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="@mipmap/bj_2" 7 > 8 9 <!--标题栏--> 10 <include layout="@layout/titlebar" /> 11 <RadioGroup 12 android:id="@+id/RadioGroup1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:orientation="horizontal" 16 > 17 <RadioButton 18 android:id="@+id/shouru" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:text="收 入 " 22 android:textSize="20dp"/> 23 <RadioButton 24 android:id="@+id/zhichu" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:text="支 出" 28 android:textSize="20dp"/> 29 </RadioGroup> 30 31 <LinearLayout 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:orientation="horizontal"> 35 <TextView 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:text="金额:" 39 android:textSize="20dp" 40 android:height="100px"/> 41 <EditText 42 android:id="@+id/edit_money" 43 android:layout_width="280sp" 44 android:layout_height="wrap_content" 45 android:hint="请输入金额" 46 android:maxLines="1" 47 android:scrollbars="vertical" 48 /> 49 </LinearLayout> 50 <LinearLayout 51 android:layout_width="wrap_content" 52 android:layout_height="wrap_content" 53 android:orientation="horizontal"> 54 <TextView 55 android:layout_width="wrap_content" 56 android:layout_height="wrap_content" 57 android:text="日期:" 58 android:textSize="20dp" 59 android:height="100px"/> 60 <EditText 61 android:id="@+id/edit_date" 62 android:layout_width="280sp" 63 android:layout_height="wrap_content" 64 android:hint="请输入时间" 65 android:maxLines="1" 66 android:scrollbars="vertical" 67 /> 68 </LinearLayout> 69 <LinearLayout 70 android:layout_width="wrap_content" 71 android:layout_height="wrap_content" 72 android:orientation="horizontal"> 73 <TextView 74 android:layout_width="wrap_content" 75 android:layout_height="wrap_content" 76 android:text="类别:" 77 android:textSize="20dp" 78 android:height="100px"/> 79 <EditText 80 android:id="@+id/edit_type" 81 android:layout_width="280sp" 82 android:layout_height="wrap_content" 83 android:hint="请输入类别" 84 android:maxLines="1" 85 android:scrollbars="vertical" 86 /> 87 </LinearLayout> 88 <LinearLayout 89 android:layout_width="wrap_content" 90 android:layout_height="wrap_content" 91 android:orientation="horizontal"> 92 <TextView 93 android:layout_width="wrap_content" 94 android:layout_height="wrap_content" 95 android:text="方式:" 96 android:textSize="20dp" 97 android:height="100px"/> 98 <EditText 99 android:id="@+id/edit_way" 100 android:layout_width="280sp" 101 android:layout_height="wrap_content" 102 android:hint="请输入支付方式" 103 android:maxLines="1" 104 android:scrollbars="vertical" 105 /> 106 </LinearLayout> 107 <LinearLayout 108 android:id="@+id/ld_btn" 109 android:layout_width="match_parent" 110 android:layout_height="wrap_content" 111 android:layout_centerVertical="true" 112 android:orientation="horizontal"> 113 114 <Button 115 android:id="@+id/btn_add" 116 android:layout_width="wrap_content" 117 android:layout_height="wrap_content" 118 android:layout_weight="1" 119 android:textSize="20sp" 120 android:text="添加" /> 121 122 <Button 123 android:id="@+id/btn_update" 124 android:layout_width="wrap_content" 125 android:layout_height="wrap_content" 126 android:layout_weight="1" 127 android:textSize="20sp" 128 android:text="修改" /> 129 130 <Button 131 android:id="@+id/btn_query" 132 android:layout_width="wrap_content" 133 android:layout_height="wrap_content" 134 android:textSize="20sp" 135 android:layout_weight="1" 136 android:text="查询" /> 137 138 <Button 139 android:id="@+id/btn_delete" 140 android:layout_width="wrap_content" 141 android:layout_height="wrap_content" 142 android:layout_weight="1" 143 android:textSize="20sp" 144 android:text="删除" /> 145 </LinearLayout> 146 147 <TextView 148 android:id="@+id/ld_show" 149 android:layout_width="match_parent" 150 android:layout_height="wrap_content" 151 android:layout_marginTop="25dp" 152 android:layout_below="@+id/ld_btn" 153 android:textSize="20sp" /> 154 155 </LinearLayout>
截图展示:
记账:
修改账单信息:
删除账单信息:
5.查看账单功能实现
showActivity文件代码:
1 package com.example.keep;
2
3 import android.database.Cursor;
4 import android.database.sqlite.SQLiteDatabase;
5 import android.os.Bundle;
6 import android.widget.TextView;
7
8 import androidx.annotation.Nullable;
9 import androidx.appcompat.app.AppCompatActivity;
10
11 import static com.example.keep.Constants.TABLE_NAME;
12
13 public class ShowActivity extends AppCompatActivity {
14 private DatabaseHelper myHelper;
15 public SQLiteDatabase db;
16 String str="";
17
18 protected void onCreate(@Nullable Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.activity_show);
21 final TextView tv=(TextView)findViewById(R.id.allshow);
22 myHelper=new DatabaseHelper(this);
23 SQLiteDatabase db=myHelper.getWritableDatabase();
24 Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);//读取数据库所有信息
25 if(cursor.moveToFirst()){
26 do{
27 String in_out=cursor.getString(cursor.getColumnIndex("in_out"));
28 String money=cursor.getString(cursor.getColumnIndex("money"));
29 String date=cursor.getString(cursor.getColumnIndex("date"));
30 String type=cursor.getString(cursor.getColumnIndex("type"));
31 String way=cursor.getString(cursor.getColumnIndex("way"));
32 /* Log.d("chakan","盈亏为:"+ye);
33 Log.d("chakan","原因为:"+why);
34 Log.d("chakan","金额为:"+money);
35 Log.d("chakan","时间为:"+time);*/
36 str+=in_out+"\t\t"+ money+"元\t\t"+date+"\t\t"+type+"\t\t\t"+way+"\n";//将数据库信息存到str中并换行
37 }while (cursor.moveToNext());
38 }
39 cursor.close();
40 tv.setText(str);//打印信息
41 }
42 }
activity_show.xml文件代码:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:orientation="vertical" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".ShowActivity" 8 android:background="@mipmap/bj_2"> 9 <!--标题栏--> 10 <include layout="@layout/titlebar" /> 11 <TextView 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:textSize="20sp" 15 android:text=" 金额 日期 类别 支付方式"/> 16 <TextView 17 android:id="@+id/allshow" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:layout_marginTop="25dp" 21 android:textSize="20sp" /> 22 23 </LinearLayout>
界面展示: