目录
声明:
此项目为移植《Android开发从入门到精通》项目实战篇,此项目仅用于学习。书本项目是基于Eclipse 4.2.2 + Android 5.0
开发运行环境
- 操作系统:windows 10
- JDK环境:java 8
- 开发工具:Android Studio 4.2.2
- 开发语言:java、XML、SQL
- 数据库管理软件:SQLite 3
代码树
dao层代码
DBOpenHelper.java
sql语句学习可参考
https://www.sqlite.org/cli.html
https://www.runoob.com/sqlite/sqlite-tutorial.html
public class DBOpenHelper extends SQLiteOpenHelper {
private static final int VERSION = 1; //数据库版本号
private static final String DBNAME = "account.db";//数据库名
public DBOpenHelper(Context context) { //重构基类构造函数
super(context,DBNAME,null,VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { //创建数据库
db.execSQL("create table tb_outaccount (_id integer primary key,money decimal ,time varchar(10)," +
"type varchar(10),address varchar(100),mark varchar(200))");//创建支出信息表
db.execSQL("create table tb_inaccount (_id integer primary key,money decimal,time varchar(10)," +
"type varchar(10),handler varchar(100),mark varchar(200))");//创建收入信息表
db.execSQL("create table tb_pwd(password varchar(20))");//创建密码表
db.execSQL("create table tb_flag (_id integer primary key,flag varchar(200))");//创建便签信息表
}
/**
* Called when the database needs to be upgraded. The implementation
* should use this method to drop tables, add tables, or do anything else it
* needs to upgrade to the new schema version.
*
* <p>
* The SQLite ALTER TABLE documentation can be found
* <a href="http://sqlite.org/lang_altertable.html">here</a>. If you add new columns
* you can use ALTER TABLE to insert them into a live table. If you rename or remove columns
* you can use ALTER TABLE to rename the old table, then create the new table and then
* populate the new table with the contents of the old table.
* </p><p>
* This method executes within a transaction. If an exception is thrown, all changes
* will automatically be rolled back.
* </p>
*
* @param db The database.
* @param oldVersion The old database version.
* @param newVersion The new database version.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
FlayDAO.java
public class FlagDAO {
private DBOpenHelper helper;// 创建DBOpenHelper对象
private SQLiteDatabase db;// 创建SQLiteDatabase对象
public FlagDAO(Context context) {// 定义构造函数
helper = new DBOpenHelper(context);// 初始化DBOpenHelper对象
db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
}
/**
* 添加便签信息
*
* @param tb_flag
*/
public void add(Tb_flag tb_flag) {
db.execSQL("insert into tb_flag (_id,flag) values (?,?)", new Object[]{
tb_flag.getid(), tb_flag.getFlag()});// 执行添加便签信息操作
}
/**
* 更新便签信息
*
* @param tb_flag
*/
public void update(Tb_flag tb_flag) {
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
db.execSQL("update tb_flag set flag = ? where _id = ?", new Object[]{
tb_flag.getFlag(), tb_flag.getid()});// 执行修改便签信息操作
}
/**
* 查找便签信息
*
* @param id
* @return
*/
@SuppressLint("Range")
public Tb_flag find(int id) {
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
Cursor cursor = db.rawQuery(
"select _id,flag from tb_flag where _id = ?",
new String[]{String.valueOf(id)});// 根据编号查找便签信息,并存储到Cursor类中
if (cursor.moveToNext()) {// 遍历查找到的便签信息
// 将遍历到的便签信息存储到Tb_flag类中
return new Tb_flag(cursor.getInt(cursor.getColumnIndex("_id")),
cursor.getString(cursor.getColumnIndex("flag")));
}
cursor.close();// 关闭游标
return null;// 如果没有信息,则返回null
}
/**
* 刪除便签信息
*
* @param ids
*/
public void delete(Integer... ids) {
if (ids.length > 0) {// 判断是否存在要删除的id
StringBuffer sb = new StringBuffer();// 创建StringBuffer对象
for (int i = 0; i < ids.length; i++) {// 遍历要删除的id集合
sb.append('?').append(',');// 将删除条件添加到StringBuffer对象中
}
sb.deleteCharAt(sb.length() - 1);// 去掉最后一个“,“字符
// db = helper.getWritableDatabase();// 创建SQLiteDatabase对象
// 执行删除便签信息操作
db.execSQL("delete from tb_flag where _id in (" + sb + ")",
(Object[]) ids);
}
}
/**
* 获取便签信息
*
* @param start 起始位置
* @param count 每页显示数量
* @return
*/
@SuppressLint("Range")
public List<Tb_flag> getScrollData(int start, int count) {
List<Tb_flag> lisTb_flags = new ArrayList<Tb_flag>();// 创建集合对象
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
// 获取所有便签信息
Cursor cursor = db.rawQuery("select * from tb_flag limit ?,?",
new String[]{String.valueOf(start), String.valueOf(count)});
while (cursor.moveToNext()) {// 遍历所有的便签信息
// 将遍历到的便签信息添加到集合中
lisTb_flags.add(new Tb_flag(cursor.getInt(cursor
.getColumnIndex("_id")), cursor.getString(cursor
.getColumnIndex("flag"))));
}
cursor.close();// 关闭游标
return lisTb_flags;// 返回集合
}
/**
* 获取总记录数
*
* @return
*/
public long getCount() {
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
Cursor cursor = db.rawQuery("select count(_id) from tb_flag", null);// 获取便签信息的记录数
if (cursor.moveToNext()) {// 判断Cursor中是否有数据
return cursor.getLong(0);// 返回总记录数
}
cursor.close();// 关闭游标
return 0;// 如果没有数据,则返回0
}
/**
* 获取便签最大编号
*
* @return
*/
public int getMaxId() {
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
Cursor cursor = db.rawQuery("select max(_id) from tb_flag", null);// 获取便签信息表中的最大编号
while (cursor.moveToLast()) {// 访问Cursor中的最后一条数据
return cursor.getInt(0);// 获取访问到的数据,即最大编号
}
cursor.close();// 关闭游标
return 0;// 如果没有数据,则返回0
}
}
InAccountDAO.java
public class InAccountDAO {
private DBOpenHelper helper;//创建DBOpenHelper对象
private SQLiteDatabase db;//创建SQLiteDatabase对象
public InAccountDAO(Context context) {
helper = new DBOpenHelper(context);//初始化DBOpenHelper对象
}
/**
* 添加收入信息
*
* @param tb_inAccount
*/
public void add(Tb_inAccount tb_inAccount) {
db = helper.getWritableDatabase(); //初始化SQLiteDatabase对象
//执行添加收入信心操作
db.execSQL("insert into tb_inaccount (_id,money,time,type,handler,mark) values (?,?,?,?,?,?)", new Object[]{
tb_inAccount.get_id(), tb_inAccount.getMoney(),
tb_inAccount.getTime(), tb_inAccount.getType(),
tb_inAccount.getHandler(), tb_inAccount.getMark()
});
}
/**
* 更新收入信息
*
* @param tb_inAccount
*/
public void update(Tb_inAccount tb_inAccount) {
db = helper.getWritableDatabase(); //初始化SQLiteDatabase对象
//执行修改收入信息操作
db.execSQL("update tb_inaccount set money = ? ,time = ? , type = ? , handler = ?,mark = ? where _id = ?", new Object[]{
tb_inAccount.getMark(), tb_inAccount.getTime(), tb_inAccount.getType(),
tb_inAccount.getHandler(), tb_inAccount.getMark(), tb_inAccount.get_id()
});
}
/**
* 查找收入信息
*
* @param id
* @return
*/
@SuppressLint("Range")
public Tb_inAccount find(int id) {
db = helper.getWritableDatabase(); //初始化SQLiteDatabase对象
Cursor cursor = db.rawQuery("select _id,money,time,type,handler,mark from tb_inaccount where _id = ?", new String[]{
String.valueOf(id)
});//根据编号查找收入信息,并存储到Cursor类中
if (cursor.moveToNext()) { //遍历查找到的收入信息
//将遍历到的收入信息存储到Tb_inAccount类中
return new Tb_inAccount(cursor.getInt(cursor.getColumnIndex("_id")), cursor.getDouble(cursor.getColumnIndex("money")),
cursor.getString(cursor.getColumnIndex("time")), cursor.getString(cursor.getColumnIndex("type")),
cursor.getString(cursor.getColumnIndex("handler")), cursor.getString(cursor.getColumnIndex("mark")));
}
return null;//如果没有信息,则返回null
}
/**
* 收入信息汇总
*
* @return
*/
public Map<String, Float> getTotal() {
List<Tb_inAccount> tb_inaccount = new ArrayList<Tb_inAccount>();// 创建集合对象
db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
// 获取所有收入汇总信息
Cursor cursor = db.rawQuery("select type,sum(money) "
+ "from tb_inaccount group by type", null);
int count = 0;
count = cursor.getCount();
Map<String, Float> map = new HashMap<String, Float>(); //创建一个Map对象
cursor.moveToFirst(); //移动第一条记录
for (int i = 0; i < count; i++) {// 遍历所有的收入汇总信息
map.put(cursor.getString(0), cursor.getFloat(1));
System.out.println("收入:" + cursor.getString(0));
cursor.moveToNext();//移到下条记录
}
cursor.close();// 关闭游标
return map;// 返回Map对象
}
/**
* 删除收入信息
*
* @param ids
*/
public void delete(Integer... ids) {
if (ids.length > 0) {//判断是否存在要删除的id
StringBuffer sb = new StringBuffer();//创建StringBuffer对象
for (int i = 0; i < ids.length; i++) {//遍历要删除的id集合
sb.append('?').append(',');//将删除条件添加到StringBuffer对象中
}
sb.deleteCharAt(sb.length() - 1);//去掉最后一个","字符
db = helper.getWritableDatabase();//初始化SQLiteDatabase对象
//执行删除收入信息操作
db.execSQL("delete from tb_inaccount where _id in (" + sb + ")", (Object[]) ids);
}
}
/**
* 获取收入信息
*
* @param start
* @param count
* @return
*/
@SuppressLint("Range")
public List<Tb_inAccount> getScrollData(int start, int count) {
List<Tb_inAccount> tb_inAccounts = new ArrayList<>();//创建集合对象
db = helper.getWritableDatabase();//初始化SQLiteDatabase对象
Cursor cursor = db.rawQuery("select * from tb_inaccount limit ?,?", new String[]{String.valueOf(start), String.valueOf(count)});//获取所有收入信息
while (cursor.moveToNext()) {//遍历所有的收入信息
tb_inAccounts.add(new Tb_inAccount(cursor.getInt(cursor.getColumnIndex("_id")), cursor.getDouble(cursor.getColumnIndex("money")),
cursor.getString(cursor.getColumnIndex("time")), cursor.getString(cursor.getColumnIndex("type")),
cursor.getString(cursor.getColumnIndex("handler")), cursor.getString(cursor.getColumnIndex("mark"))));//将遍历到的收入信息添加到集合中
}
return tb_inAccounts;//返回集合
}
/**
* 获取总记录数
*
* @return
*/
public long getCount() {
db = helper.getWritableDatabase();//初始化SQLiteDatabase对象
Cursor cursor = db.rawQuery("select count(_id) from tb_inaccount", null);//获取收入信息的记录数
if (cursor.moveToNext()) {//判断Cursor中是否有数据
return cursor.getLong(0);//返回总记录数
}
return 0;//如果没有记录数,则返回0
}
/**
* 获取收入最大编号
*
* @return
*/
public int getMaxId() {
db = helper.getWritableDatabase();//初始化SQLiteDatabase对象
Cursor cursor = db.rawQuery("select max(_id) from tb_inaccount", null);//获取收入信息表中的最大编号
while (cursor.moveToLast()) {//获取Cursor中的最后一条数据
return cursor.getInt(0);//获取访问到的数据,即最大编号
}
return 0;//如果没有数据,返回0
}
}
OutAccountDAO.java
public class OutAccountDAO {
private DBOpenHelper helper;// 创建DBOpenHelper对象
private SQLiteDatabase db;// 创建SQLiteDatabase对象
public OutAccountDAO(Context context) {// 定义构造函数
helper = new DBOpenHelper(context);// 初始化DBOpenHelper对象
db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
}
/**
* 添加支出信息
*
* @param tb_outaccount
*/
public void add(Tb_outaccount tb_outaccount) {
// 执行添加支出信息操作
db.execSQL(
"insert into tb_outaccount (_id,money,time,type,address,mark) values (?,?,?,?,?,?)",
new Object[]{tb_outaccount.getid(), tb_outaccount.getMoney(),
tb_outaccount.getTime(), tb_outaccount.getType(),
tb_outaccount.getAddress(), tb_outaccount.getMark()});
}
/**
* 更新支出信息
*
* @param tb_outaccount
*/
public void update(Tb_outaccount tb_outaccount) {
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
// 执行修改支出信息操作
db.execSQL(
"update tb_outaccount set money = ?,time = ?,type = ?,address = ?,mark = ? where _id = ?",
new Object[]{tb_outaccount.getMoney(),
tb_outaccount.getTime(), tb_outaccount.getType(),
tb_outaccount.getAddress(), tb_outaccount.getMark(),
tb_outaccount.getid()});
}
/**
* 查找支出信息
*
* @param id
* @return
*/
@SuppressLint("Range")
public Tb_outaccount find(int id) {
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
Cursor cursor = db
.rawQuery(
"select _id,money,time,type,address,mark from tb_outaccount where _id = ?",
new String[]{String.valueOf(id)});// 根据编号查找支出信息,并存储到Cursor类中
if (cursor.moveToNext()) {// 遍历查找到的支出信息
// 将遍历到的支出信息存储到Tb_outaccount类中
return new Tb_outaccount(
cursor.getInt(cursor.getColumnIndex("_id")),
cursor.getDouble(cursor.getColumnIndex("money")),
cursor.getString(cursor.getColumnIndex("time")),
cursor.getString(cursor.getColumnIndex("type")),
cursor.getString(cursor.getColumnIndex("address")),
cursor.getString(cursor.getColumnIndex("mark")));
}
cursor.close();// 关闭游标
return null;// 如果没有信息,则返回null
}
/**
* 刪除支出信息
*
* @param ids
*/
public void delete(Integer... ids) {
if (ids.length > 0) {// 判断是否存在要删除的id
StringBuffer sb = new StringBuffer();// 创建StringBuffer对象
for (int i = 0; i < ids.length; i++) {// 遍历要删除的id集合
sb.append('?').append(',');// 将删除条件添加到StringBuffer对象中
}
sb.deleteCharAt(sb.length() - 1);// 去掉最后一个“,“字符
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
// 执行删除支出信息操作
db.execSQL("delete from tb_outaccount where _id in (" + sb + ")",
(Object[]) ids);
}
}
/**
* 支出信息汇总
*
* @return
*/
public Map<String, Float> getTotal() {
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
// 获取所有支出汇总信息
Cursor cursor = db.rawQuery("select type,sum(money) from tb_outaccount group by type", null);
int count = 0;
count = cursor.getCount();
Map<String, Float> map = new HashMap<String, Float>(); //创建一个Map对象
cursor.moveToFirst(); //移动第一条记录
for (int i = 0; i < count; i++) {// 遍历所有的收入汇总信息
map.put(cursor.getString(0), cursor.getFloat(1));
System.out.println("支出:" + cursor.getString(0) + cursor.getFloat(1));
cursor.moveToNext();//移到下条记录
}
cursor.close();// 关闭游标
return map;// 返回Map对象
}
/**
* 获取支出信息
*
* @param start 起始位置
* @param count 每页显示数量
* @return
*/
@SuppressLint("Range")
public List<Tb_outaccount> getScrollData(int start, int count) {
List<Tb_outaccount> tb_outaccount = new ArrayList<Tb_outaccount>();// 创建集合对象
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
// 获取所有支出信息
Cursor cursor = db.rawQuery("select * from tb_outaccount limit ?,?",
new String[]{String.valueOf(start), String.valueOf(count)});
while (cursor.moveToNext()) {// 遍历所有的支出信息
// 将遍历到的支出信息添加到集合中
tb_outaccount.add(new Tb_outaccount(cursor.getInt(cursor
.getColumnIndex("_id")), cursor.getDouble(cursor
.getColumnIndex("money")), cursor.getString(cursor
.getColumnIndex("time")), cursor.getString(cursor
.getColumnIndex("type")), cursor.getString(cursor
.getColumnIndex("address")), cursor.getString(cursor
.getColumnIndex("mark"))));
}
cursor.close();// 关闭游标
return tb_outaccount;// 返回集合
}
/**
* 获取总记录数
*
* @return
*/
public long getCount() {
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
Cursor cursor = db.rawQuery("select count(_id) from tb_outaccount",
null);// 获取支出信息的记录数
if (cursor.moveToNext()) {// 判断Cursor中是否有数据
return cursor.getLong(0);// 返回总记录数
}
cursor.close();// 关闭游标
return 0;// 如果没有数据,则返回0
}
/**
* 获取支出最大编号
*
* @return
*/
public int getMaxId() {
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
Cursor cursor = db.rawQuery("select max(_id) from tb_outaccount", null);// 获取支出信息表中的最大编号
while (cursor.moveToLast()) {// 访问Cursor中的最后一条数据
return cursor.getInt(0);// 获取访问到的数据,即最大编号
}
cursor.close();// 关闭游标
return 0;// 如果没有数据,则返回0
}
}
PwdDAO.java
public class PwdDAO {
private DBOpenHelper helper;// 创建DBOpenHelper对象
private SQLiteDatabase db;// 创建SQLiteDatabase对象
public PwdDAO(Context context) {// 定义构造函数
helper = new DBOpenHelper(context);// 初始化DBOpenHelper对象
db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
}
/**
* 添加密码信息
*
* @param tb_pwd
*/
public void add(Tb_pwd tb_pwd) {
// 执行添加密码操作
db.execSQL("insert into tb_pwd (password) values (?)",
new Object[]{tb_pwd.getPassword()});
}
/**
* 设置密码信息
*
* @param tb_pwd
*/
public void update(Tb_pwd tb_pwd) {
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
// 执行修改密码操作
db.execSQL("update tb_pwd set password = ?",
new Object[]{tb_pwd.getPassword()});
}
/**
* 查找密码信息
*
* @return
*/
@SuppressLint("Range")
public Tb_pwd find() {
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
// 查找密码并存储到Cursor类中
Cursor cursor = db.rawQuery("select password from tb_pwd", null);
if (cursor.moveToNext()) {// 遍历查找到的密码信息
// 将密码存储到Tb_pwd类中
return new Tb_pwd(cursor.getString(cursor
.getColumnIndex("password")));
}
cursor.close();// 关闭游标
return null;// 如果没有信息,则返回null
}
public long getCount() {
// db = helper.getWritableDatabase();// 初始化SQLiteDatabase对象
Cursor cursor = db.rawQuery("select count(password) from tb_pwd", null);// 获取密码信息的记录数
if (cursor.moveToNext()) {// 判断Cursor中是否有数据
return cursor.getLong(0);// 返回总记录数
}
cursor.close();// 关闭游标
return 0;// 如果没有数据,则返回0
}
}
model层代码
Tb_flag.java
public class Tb_flag {
private int _id;// 存储便签编号
private String flag;// 存储便签信息
public Tb_flag(){// 默认构造函数
super();
}
// 定义有参构造函数,用来初始化便签信息实体类中的各个字段
public Tb_flag(int id, String flag) {
super();
this._id = id;// 为便签号赋值
this.flag = flag;// 为便签信息赋值
}
public int getid(){// 设置便签编号的可读属性
return _id;
}
public void setid(int id){// 设置便签编号的可写属性
this._id = id;
}
public String getFlag(){// 设置便签信息的可读属性
return flag;
}
public void setFlag(String flag){// 设置便签信息的可写属性
this.flag = flag;
}
}
Tb_inAccount.java
public class Tb_inAccount {
private int _id; //存储收入编号
private double money; //存储收入金额
private String time; //存储收入时间
private String type; //存储收入类别
private String handler; //存储收入付款方
private String mark; //存储收入备注
public Tb_inAccount(){// 默认构造函数
super();
}
public Tb_inAccount(int _id, double money, String time, String type, String handler, String mark) {
super();
this._id = _id;
this.money = money;
this.time = time;
this.type = type;
this.handler = handler;
this.mark = mark;
}
public int get_id() {
return _id;
}
public double getMoney() {
return money;
}
public String getTime() {
return time;
}
public String getType() {
return type;
}
public String getHandler() {
return handler;
}
public String getMark() {
return mark;
}
public void set_id(int _id) {
this._id = _id;
}
public void setMoney(double money) {
this.money = money;
}
public void setTime(String time) {
this.time = time;
}
public void setType(String type) {
this.type = type;
}
public void setHandler(String handler) {
this.handler = handler;
}
public void setMark(String mark) {
this.mark = mark;
}
}
Tb_outAccount.java
public class Tb_outAccount {
private int _id;// 存储支出编号
private double money;// 存储支出金额
private String time;// 存储支出时间
private String type;// 存储支出类别
private String address;// 存储支出地点
private String mark;// 存储支出备注
public Tb_outAccount(){// 默认构造函数
super();
}
// 定义有参构造函数,用来初始化支出信息实体类中的各个字段
public Tb_outAccount(int id, double money, String time, String type,
String address, String mark) {
super();
this._id = id;// 为支出编号赋值
this.money = money;// 为支出金额赋值
this.time = time;// 为支出时间赋值
this.type = type;// 为支出类别赋值
this.address = address;// 为支出地点赋值
this.mark = mark;// 为支出备注赋值
}
public int getid(){// 设置支出编号的可读属性
return _id;
}
public void setid(int id){// 设置支出编号的可写属性
this._id = id;
}
public double getMoney(){// 设置支出金额的可读属性
return money;
}
public void setMoney(double money){// 设置支出金额的可写属性
this.money = money;
}
public String getTime(){// 设置支出时间的可读属性
return time;
}
public void setTime(String time){// 设置支出时间的可写属性
this.time = time;
}
public String getType(){// 设置支出类别的可读属性
return type;
}
public void setType(String type){// 设置支出类别的可写属性
this.type = type;
}
public String getAddress(){// 设置支出地点的可读属性
return address;
}
public void setAddress(String address){// 设置支出地点的可写属性
this.address = address;
}
public String getMark(){// 设置支出备注的可读属性
return mark;
}
public void setMark(String mark){// 设置支出备注的可写属性
this.mark = mark;
}
}
Tb_pwd.java
public class Tb_pwd {
private String password;// 定义字符串,表示用户密码
public Tb_pwd(){// 默认构造函数
super();
}
public Tb_pwd(String password){// 定义有参构造函数
super();
this.password = password;// 为密码赋值
}
public String getPassword(){// 定义密码的可读属性
return password;
}
public void setPassword(String password){// 定义密码的可写属性
this.password = password;
}
}
Activity层代码块
MainActivity.java
显示界面
public class MainActivity extends AppCompatActivity {
private final String[] titles = new String[]{"新增支出","新增收入","我的支出","我的收入","数据管理","系统设置","收入便签","帮助","退出"};
private final int[] images = new int[]{R.drawable.addoutaccount,R.drawable.addinaccount,
R.drawable.outaccountinfo,R.drawable.inaccountinfo,R.drawable.showinfo,
R.drawable.sysset,R.drawable.accountflag,R.drawable.help,R.drawable.exit};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gvInfo = findViewById(R.id.gvInfo);
pictureAdapter adapter = new pictureAdapter(titles, images, this);
gvInfo.setAdapter(adapter);
gvInfo.setOnItemClickListener((parent, view, position, id) -> {
Intent intent;
switch (position){
case 0:
intent = new Intent(MainActivity.this, AddOutAccount.class);
startActivity(intent);
break;
case 1:
intent = new Intent(MainActivity.this, AddInAccount.class);
startActivity(intent);
break;
case 2:
intent = new Intent(MainActivity.this, OutAccountInfo.class);// 使用Outaccountinfo窗口初始化Intent
startActivity(intent);// 打开Outaccountinfo
break;
case 3:
intent = new Intent(MainActivity.this, InAccountInfo.class);// 使用Inaccountinfo窗口初始化Intent
startActivity(intent);// 打开Inaccountinfo
break;
case 4:
intent = new Intent(MainActivity.this, Showinfo.class);// 使用Showinfo窗口初始化Intent
startActivity(intent);// 打开Showinfo
break;
case 5:
intent = new Intent(MainActivity.this, Sysset.class);// 使用Sysset窗口初始化Intent
startActivity(intent);// 打开Sysset
break;
case 6:
intent = new Intent(MainActivity.this, Accountflag.class);// 使用Accountflag窗口初始化Intent
startActivity(intent);// 打开Accountflag
break;
case 7:
intent = new Intent(MainActivity.this, Help.class);// 使用Help窗口初始化Intent
startActivity(intent);// 打开Help
break;
case 8:
finish();// 关闭当前Activity
System.exit(0);
}
});
}
static class pictureAdapter extends BaseAdapter {// 创建基于BaseAdapter的子类
private final LayoutInflater inflater;// 创建LayoutInflater对象
private final List<Picture> pictures;// 创建List泛型集合
// 为类创建构造函数
public pictureAdapter(String[] titles, int[] images, Context context) {
super();
pictures = new ArrayList<>();// 初始化泛型集合对象
inflater = LayoutInflater.from(context);// 初始化LayoutInflater对象
for (int i = 0; i < images.length; i++){// 遍历图像数组
Picture picture = new Picture(titles[i], images[i]);// 使用标题和图像生成Picture对象
pictures.add(picture);// 将Picture对象添加到泛型集合中
}
}
@Override
public int getCount() {// 获取泛型集合的长度
if (null != pictures) {// 如果泛型集合不为空
return pictures.size();// 返回泛型长度
} else {
return 0;// 返回0
}
}
@Override
public Object getItem(int arg0) {
return pictures.get(arg0);// 获取泛型集合指定索引处的项
}
@Override
public long getItemId(int arg0) {
return arg0;// 返回泛型集合的索引
}
@SuppressLint("InflateParams")
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ViewHolder viewHolder;// 创建ViewHolder对象
if (arg1 == null){// 判断图像标识是否为空
arg1 = inflater.inflate(R.layout.gvitem, null);// 设置图像标识
viewHolder = new ViewHolder();// 初始化ViewHolder对象
viewHolder.title = arg1.findViewById(R.id.ItemTitle);// 设置图像标题
viewHolder.image = arg1.findViewById(R.id.ItemImage);// 设置图像的二进制值
arg1.setTag(viewHolder);// 设置提示
} else {
viewHolder = (ViewHolder) arg1.getTag();// 设置提示
}
viewHolder.title.setText(pictures.get(arg0).getTitle());// 设置图像标题
viewHolder.image.setImageResource(pictures.get(arg0).getImageId());// 设置图像的二进制值
return arg1;// 返回图像标识
}
}
static class ViewHolder{// 创建ViewHolder类
public TextView title;// 创建TextView对象
public ImageView image;// 创建ImageView对象
}
static class Picture {// 创建Picture类
private String title;// 定义字符串,表示图像标题
private int imageId;// 定义int变量,表示图像的二进制值
public Picture() {// 默认构造函数
super();
}
public Picture(String title, int imageId) {// 定义有参构造函数
super();
this.title = title;// 为图像标题赋值
this.imageId = imageId;// 为图像的二进制值赋值
}
public String getTitle() {// 定义图像标题的可读属性
return title;
}
public void setTitle(String title) {// 定义图像标题的可写属性
this.title = title;
}
public int getImageId() {// 定义图像二进制值的可读属性
return imageId;
}
public void setimageId(int imageId) {// 定义图像二进制值的可写属性
this.imageId = imageId;
}
}
}
AddOutAccount.java
public class AddOutAccount extends Activity {
protected static final int DATE_DIALOG_ID = 0;// 创建日期对话框常量
EditText txtMoney, txtTime, txtAddress, txtMark;// 创建4个EditText对象
Spinner spType;// 创建Spinner对象
Button btnSaveButton;// 创建Button对象“保存”
Button btnCancelButton;// 创建Button对象“取消”
private int mYear;// 年
private int mMonth;// 月
private int mDay;// 日
private int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addoutaccount);// 设置布局文件
txtMoney = findViewById(R.id.txtMoney);// 获取金额文本框
txtTime = findViewById(R.id.txtTime);// 获取时间文本框
txtAddress = findViewById(R.id.txtAddress);// 获取地点文本框
txtMark = findViewById(R.id.txtMark);// 获取备注文本框
spType = findViewById(R.id.spType);// 获取类别下拉列表
btnSaveButton = findViewById(R.id.btnSave);// 获取保存按钮
btnCancelButton = findViewById(R.id.btnCancel);// 获取取消按钮
// 为时间文本框设置单击监听事件
txtTime.setOnClickListener(arg0 -> {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);// 显示日期选择对话框
});
// 为保存按钮设置监听事件
btnSaveButton.setOnClickListener(arg0 -> {
// TODO Auto-generated method stub
String strMoney = txtMoney.getText().toString();// 获取金额文本框的值
if (!strMoney.isEmpty()) {// 判断金额不为空
// 创建OutaccountDAO对象
OutAccountDAO outaccountDAO = new OutAccountDAO(
AddOutAccount.this);
// 创建Tb_outaccount对象
Tb_outAccount tb_outaccount = new Tb_outAccount(
outaccountDAO.getMaxId() + 1, Double
.parseDouble(strMoney), txtTime
.getText().toString(), spType
.getSelectedItem().toString(),
txtAddress.getText().toString(), txtMark
.getText().toString());
outaccountDAO.add(tb_outaccount);// 添加支出信息
// 弹出信息提示
Toast.makeText(AddOutAccount.this, "〖新增支出〗数据添加成功!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AddOutAccount.this, "请输入支出金额!",
Toast.LENGTH_SHORT).show();
}
});
// 为取消按钮设置监听事件
btnCancelButton.setOnClickListener(arg0 -> {
// TODO Auto-generated method stub
txtMoney.setText("");// 设置金额文本框为空
txtMoney.setHint("0.00");// 为金额文本框设置提示
txtTime.setText("");// 设置时间文本框为空
txtTime.setHint("2017-01-01");// 为时间文本框设置提示
txtAddress.setText("");// 设置地点文本框为空
txtMark.setText("");// 设置备注文本框为空
spType.setSelection(0);// 设置类别下拉列表默认选择第一项
});
final Calendar c = Calendar.getInstance();// 获取当前系统日期
mYear = c.get(Calendar.YEAR);// 获取年份
mMonth = c.get(Calendar.MONTH);// 获取月份
mDay = c.get(Calendar.DAY_OF_MONTH);// 获取天数
updateDisplay();// 显示当前系统时间
}
@Override
protected Dialog onCreateDialog(int id){
this.id = id;// 重写onCreateDialog方法
switch (id) {
case DATE_DIALOG_ID:// 弹出日期选择对话框
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
/**
* @param view the picker associated with the dialog
* @param year the selected year
* @param monthOfYear the selected month (0-11 for compatibility with
* {@link Calendar#MONTH})
* @param dayOfMonth the selected day of the month (1-31, depending on
*/
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;// 为年份赋值
mMonth = monthOfYear;// 为月份赋值
mDay = dayOfMonth;// 为天赋值
updateDisplay();// 显示设置的日期
}
};
private void updateDisplay() {
// 显示设置的时间
txtTime.setText(new StringBuilder().append(mYear).append("-")
.append(mMonth + 1).append("-").append(mDay));
}
}
AddInAccount.java
public class AddInAccount extends Activity {
protected static final int DATE_DIALOG_ID = 0;// 创建日期对话框常量
EditText txtInMoney, txtInTime, txtInHandler, txtInMark;// 创建4个EditText对象
Spinner spInType;// 创建Spinner对象
Button btnInSaveButton;// 创建Button对象“保存”
Button btnInCancelButton;// 创建Button对象“取消”
private int mYear;// 年
private int mMonth;// 月
private int mDay;// 日
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addinaccount);// 设置布局文件
txtInMoney = findViewById(R.id.txtInMoney);// 获取金额文本框
txtInTime = findViewById(R.id.txtInTime);// 获取时间文本框
txtInHandler = findViewById(R.id.txtInHandler);// 获取付款方文本框
txtInMark = findViewById(R.id.txtInMark);// 获取备注文本框
spInType = findViewById(R.id.spInType);// 获取类别下拉列表
btnInSaveButton = findViewById(R.id.btnInSave);// 获取保存按钮
btnInCancelButton = findViewById(R.id.btnInCancel);// 获取取消按钮
txtInTime.setOnClickListener(new View.OnClickListener() {// 为时间文本框设置单击监听事件
@Override
public void onClick(View arg0) {
showDialog(DATE_DIALOG_ID);// 显示日期选择对话框
}
});
// 为保存按钮设置监听事件
btnInSaveButton.setOnClickListener(arg0 -> {
String strInMoney = txtInMoney.getText().toString();// 获取金额文本框的值
if (!strInMoney.isEmpty()) {// 判断金额不为空
// 创建InaccountDAO对象
InAccountDAO inaccountDAO = new InAccountDAO(
AddInAccount.this);
// 创建Tb_inaccount对象
Tb_inAccount tb_inaccount = new Tb_inAccount(
inaccountDAO.getMaxId() + 1, Double
.parseDouble(strInMoney), txtInTime
.getText().toString(), spInType
.getSelectedItem().toString(),
txtInHandler.getText().toString(),
txtInMark.getText().toString());
inaccountDAO.add(tb_inaccount);// 添加收入信息
// 弹出信息提示
Toast.makeText(AddInAccount.this, "〖新增收入〗数据添加成功!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AddInAccount.this, "请输入收入金额!",
Toast.LENGTH_SHORT).show();
}
});
// 为取消按钮设置监听事件
btnInCancelButton.setOnClickListener(arg0 -> {
txtInMoney.setText("");// 设置金额文本框为空
txtInMoney.setHint("0.00");// 为金额文本框设置提示
txtInTime.setText("");// 设置时间文本框为空
txtInTime.setHint("2017-01-01");// 为时间文本框设置提示
txtInHandler.setText("");// 设置付款方文本框为空
txtInMark.setText("");// 设置备注文本框为空
spInType.setSelection(0);// 设置类别下拉列表默认选择第一项
});
final Calendar c = Calendar.getInstance();// 获取当前系统日期
mYear = c.get(Calendar.YEAR);// 获取年份
mMonth = c.get(Calendar.MONTH);// 获取月份
mDay = c.get(Calendar.DAY_OF_MONTH);// 获取天数
updateDisplay();// 显示当前系统时间
}
@Override
protected Dialog onCreateDialog(int id){// 重写onCreateDialog方法
switch (id) {
case DATE_DIALOG_ID:// 弹出日期选择对话框
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
/**
* @param view the picker associated with the dialog
* @param year the selected year
* @param monthOfYear the selected month (0-11 for compatibility with
* {@link Calendar#MONTH})
* @param dayOfMonth the selected day of the month (1-31, depending on
*/
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;// 为年份赋值
mMonth = monthOfYear;// 为月份赋值
mDay = dayOfMonth;// 为天赋值
updateDisplay();// 显示设置的日期
}
};
private void updateDisplay() {
// 显示设置的时间
txtInTime.setText(new StringBuilder().append(mYear).append("-")
.append(mMonth + 1).append("-").append(mDay));
}
}
OutAccountInfo.java
public class OutAccountInfo extends Activity {
public static final String FLAG = "id";// 定义一个常量,用来作为请求码
ListView lvinfo;// 创建ListView对象
String strType = "";// 创建字符串,记录管理类型
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.outaccountinfo);// 设置布局文件
lvinfo = (ListView) findViewById(R.id.lvoutaccountinfo);// 获取布局文件中的ListView组件
ShowInfo(R.id.btnoutinfo);// 调用自定义方法显示支出信息
lvinfo.setOnItemClickListener(new AdapterView.OnItemClickListener(){// 为ListView添加项单击事件
// 重写onItemClick方法
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String strInfo = String.valueOf(((TextView) view).getText());// 记录支出信息
String strid = strInfo.substring(0, strInfo.indexOf('|'));// 从支出信息中截取支出编号
Intent intent = new Intent(OutAccountInfo.this,
InfoManage.class);// 创建Intent对象
intent.putExtra(FLAG, new String[] { strid, strType });// 设置传递数据
startActivity(intent);// 执行Intent操作
}
});
}
private void ShowInfo(int intType) {// 用来根据传入的管理类型,显示相应的信息
String[] strInfos;// 定义字符串数组,用来存储支出信息
ArrayAdapter<String> arrayAdapter;// 创建ArrayAdapter对象
strType = "btnoutinfo";// 为strType变量赋值
OutAccountDAO outaccountinfo = new OutAccountDAO(OutAccountInfo.this);// 创建OutaccountDAO对象
// 获取所有支出信息,并存储到List泛型集合中
List<Tb_outAccount> listoutinfos = outaccountinfo.getScrollData(0,
(int) outaccountinfo.getCount());
strInfos = new String[listoutinfos.size()];// 设置字符串数组的长度
int i = 0;// 定义一个开始标识
for (Tb_outAccount tb_outaccount : listoutinfos) {// 遍历List泛型集合
// 将支出相关信息组合成一个字符串,存储到字符串数组的相应位置
strInfos[i] = tb_outaccount.getid() + "|" + tb_outaccount.getType()
+ " " + String.valueOf(tb_outaccount.getMoney()) + "元 "
+ tb_outaccount.getTime();
i++;// 标识加1
}
// 使用字符串数组初始化ArrayAdapter对象
arrayAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, strInfos);
lvinfo.setAdapter(arrayAdapter);// 为ListView列表设置数据源
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();// 实现基类中的方法
ShowInfo(R.id.btnoutinfo);// 显示收入信息
}
}
InAccountInfo.java
public class InAccountInfo extends Activity {
public static final String FLAG = "id";// 定义一个常量,用来作为请求码
ListView lvinfo;// 创建ListView对象
String strType = "";// 创建字符串,记录管理类型
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.inaccountinfo);// 设置布局文件
lvinfo = (ListView) findViewById(R.id.lvinaccountinfo);// 获取布局文件中的ListView组件
ShowInfo(R.id.btnininfo);// 调用自定义方法显示收入信息
lvinfo.setOnItemClickListener(new AdapterView.OnItemClickListener(){// 为ListView添加项单击事件
// 重写onItemClick方法
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String strInfo = String.valueOf(((TextView) view).getText());// 记录收入信息
String strid = strInfo.substring(0, strInfo.indexOf('|'));// 从收入信息中截取收入编号
Intent intent = new Intent(InAccountInfo.this, InfoManage.class);// 创建Intent对象
intent.putExtra(FLAG, new String[] { strid, strType });// 设置传递数据
startActivity(intent);// 执行Intent操作
}
});
}
private void ShowInfo(int intType) {// 用来根据传入的管理类型,显示相应的信息
String[] strInfos = null;// 定义字符串数组,用来存储收入信息
ArrayAdapter<String> arrayAdapter = null;// 创建ArrayAdapter对象
strType = "btnininfo";// 为strType变量赋值
InAccountDAO inaccountinfo = new InAccountDAO(InAccountInfo.this);// 创建InaccountDAO对象
// 获取所有收入信息,并存储到List泛型集合中
List<Tb_inAccount> listinfos = inaccountinfo.getScrollData(0,
(int) inaccountinfo.getCount());
strInfos = new String[listinfos.size()];// 设置字符串数组的长度
int m = 0;// 定义一个开始标识
for (Tb_inAccount tb_inaccount : listinfos) {// 遍历List泛型集合
// 将收入相关信息组合成一个字符串,存储到字符串数组的相应位置
strInfos[m] = tb_inaccount.get_id() + "|" + tb_inaccount.getType()
+ " " + String.valueOf(tb_inaccount.getMoney()) + "元 "
+ tb_inaccount.getTime();
m++;// 标识加1
}
// 使用字符串数组初始化ArrayAdapter对象
arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, strInfos);
lvinfo.setAdapter(arrayAdapter);// 为ListView列表设置数据源
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();// 实现基类中的方法
ShowInfo(R.id.btnininfo);// 显示收入信息
}
}
ShowInfo.java
public class Showinfo extends Activity {
public static final String FLAG = "id";// 定义一个常量,用来作为请求码
Button btnoutinfo, btnininfo, btnflaginfo;// 创建3个Button对象
ListView lvinfo;// 创建ListView对象
String strType = "";// 创建字符串,记录管理类型
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.showinfo);// 设置布局文件
lvinfo = findViewById(R.id.lvinfo);// 获取布局文件中的ListView组件
btnoutinfo = findViewById(R.id.btnoutinfo);// 获取布局文件中的支出信息按钮
btnininfo = findViewById(R.id.btnininfo);// 获取布局文件中的收入信息按钮
btnflaginfo = findViewById(R.id.btnflaginfo);// 获取布局文件中的便签信息按钮
//ShowInfo(R.id.btnoutinfo);// 默认显示支出信息
// 为支出信息按钮设置监听事件
btnoutinfo.setOnClickListener(arg0 -> {
// TODO Auto-generated method stub
ShowInfo(R.id.btnoutinfo);// 显示支出信息
});
// 为收入信息按钮设置监听事件
btnininfo.setOnClickListener(arg0 -> {
// TODO Auto-generated method stub
ShowInfo(R.id.btnininfo);// 显示收入信息
});
// 为便签信息按钮设置监听事件
btnflaginfo.setOnClickListener(arg0 -> {
// TODO Auto-generated method stub
ShowInfo(R.id.btnflaginfo);// 显示便签信息
});
// 为ListView添加项单击事件
// 重写onItemClick方法
lvinfo.setOnItemClickListener((parent, view, position, id) -> {
String strInfo = String.valueOf(((TextView) view).getText());// 记录单击的项信息
String strid = strInfo.substring(0, strInfo.indexOf('|'));// 从项信息中截取编号
Intent intent = null;// 创建Intent对象
if (strType == "btnflaginfo") {// 判断如果是便签信息
intent = new Intent(Showinfo.this, FlagManage.class);// 使用FlagManage窗口初始化Intent对象
intent.putExtra(FLAG, strid);// 设置要传递的数据
startActivity(intent);// 执行Intent,打开相应的Activity
}
});
}
private void ShowInfo(int intType) {// 用来根据传入的管理类型,显示相应的信息
String[] strInfos = null;// 定义字符串数组,用来存储收入信息
ArrayAdapter<String> arrayAdapter;// 创建ArrayAdapter对象
Intent intent = null;// 创建Intent对象
switch (intType) {// 以intType为条件进行判断
case R.id.btnoutinfo:// 如果是支出按钮btnoutinfo
strType = "outinfo";// 为strType变量赋值
intent = new Intent(Showinfo.this, TotalChart.class);// 使用TotalChart窗口初始化Intent对象
intent.putExtra("passType", strType);// 设置要传递的数据
startActivity(intent);// 执行Intent,打开相应的Activity
break;
case R.id.btnininfo:// 如果是收入按钮btnininfo
strType = "ininfo";// 为strType变量赋值
intent = new Intent(Showinfo.this, TotalChart.class);// 使用TotalChart窗口初始化Intent对象
intent.putExtra("passType", strType);// 设置要传递的数据
startActivity(intent);// 执行Intent,打开相应的Activity
break;
case R.id.btnflaginfo:// 如果是btnflaginfo按钮
strType = "btnflaginfo";// 为strType变量赋值
FlagDAO flaginfo = new FlagDAO(Showinfo.this);// 创建FlagDAO对象
// 获取所有便签信息,并存储到List泛型集合中
List<Tb_flag> listFlags = flaginfo.getScrollData(0,
(int) flaginfo.getCount());
strInfos = new String[listFlags.size()];// 设置字符串数组的长度
int n = 0;// 定义一个开始标识
for (Tb_flag tb_flag : listFlags) {// 遍历List泛型集合
// 将便签相关信息组合成一个字符串,存储到字符串数组的相应位置
strInfos[n] = tb_flag.getid() + "|" + tb_flag.getFlag();
if (strInfos[n].length() > 30)// 判断便签信息的长度是否大于30
strInfos[n] = strInfos[n].substring(0, 30) + "……";// 将位置大于30之后的字符串用……代替
n++;// 标识加1
}
// 使用字符串数组初始化ArrayAdapter对象
arrayAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, strInfos);
lvinfo.setAdapter(arrayAdapter);// 为ListView列表设置数据源
break;
}
}
}
Sysset.java
public class Sysset extends Activity {
EditText txtpwd;// 创建EditText对象
Button btnSet, btnsetCancel;// 创建两个Button对象
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sysset);// 设置布局文件
txtpwd = (EditText) findViewById(R.id.txtPwd);// 获取密码文本框
btnSet = (Button) findViewById(R.id.btnSet);// 获取设置按钮
btnsetCancel = (Button) findViewById(R.id.btnsetCancel);// 获取取消按钮
btnSet.setOnClickListener(new View.OnClickListener() {// 为设置按钮添加监听事件
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
PwdDAO pwdDAO = new PwdDAO(Sysset.this);// 创建PwdDAO对象
Tb_pwd tb_pwd = new Tb_pwd(txtpwd.getText().toString());// 根据输入的密码创建Tb_pwd对象
if (pwdDAO.getCount() == 0) {// 判断数据库中是否已经设置了密码
pwdDAO.add(tb_pwd);// 添加用户密码
} else {
pwdDAO.update(tb_pwd);// 修改用户密码
}
// 弹出信息提示
Toast.makeText(Sysset.this, "〖密码〗设置成功!", Toast.LENGTH_SHORT)
.show();
}
});
btnsetCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
txtpwd.setText("");// 清空密码文本框
txtpwd.setHint("请输入密码");// 为密码文本框设置提示
}
});
}
}
AccountFlag.java
public class AccountFlag extends Activity {
EditText txtFlag;// 创建EditText组件对象
Button btnflagSaveButton;// 创建Button组件对象
Button btnflagCancelButton;// 创建Button组件对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accountflag);
txtFlag = findViewById(R.id.txtFlag);// 获取便签文本框
btnflagSaveButton = findViewById(R.id.btnflagSave);// 获取保存按钮
btnflagCancelButton = findViewById(R.id.btnflagCancel);// 获取取消按钮
// 为保存按钮设置监听事件
btnflagSaveButton.setOnClickListener(arg0 -> {
String strFlag = txtFlag.getText().toString();// 获取便签文本框的值
if (!strFlag.isEmpty()) {// 判断获取的值不为空
FlagDAO flagDAO = new FlagDAO(AccountFlag.this);// 创建FlagDAO对象
Tb_flag tb_flag = new Tb_flag(
flagDAO.getMaxId() + 1, strFlag);// 创建Tb_flag对象
flagDAO.add(tb_flag);// 添加便签信息
// 弹出信息提示
Toast.makeText(AccountFlag.this, "〖新增便签〗数据添加成功!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(AccountFlag.this, "请输入便签!",
Toast.LENGTH_SHORT).show();
}
});
btnflagCancelButton.setOnClickListener(new View.OnClickListener() {// 为取消按钮设置监听事件
@Override
public void onClick(View arg0) {
txtFlag.setText("");// 清空便签文本框
}
});
}
}
Help.java
public class Help extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
WebView webview= findViewById(R.id.webView1);//获取布局管理器中添加的WebView组件
//创建一个字符串构建器,将要显示的HTML内容放置在该构建器中
StringBuilder sb=new StringBuilder();
sb.append("<div>《家庭理财通》使用帮助:</div>");
sb.append("<ul>");
sb.append("<li>修改密码:选择“系统设置”模块可以修改登录密码,项目运行时,默认没有密码。</li>");
sb.append("<li>支出管理:选择“新增支出”模块可以添加支出信息;选择“我的支出”模块可以查看、修改或删除支出信息。</li>");
sb.append("<li>收入管理:选择“新增收入”模块可以添加收入信息;选择“我的收入”模块可以查看、修改或删除收入信息。</li>");
sb.append("<li>便签管理:选择“收支便签”模块可以添加便签信息;选择“数据管理”模块中的“便签信息”按钮可以查看、修改或删除便签信息。</li>");
sb.append("<li>退出系统:选择“退出”模块可以退出《家庭理财通》项目。</li>");
sb.append("</ul>");
webview.loadDataWithBaseURL(null, sb.toString(),"text/html","utf-8",null);//加载数据
}
}
数据库管理
查看数据库
View->Tool Windows->Database Inspector
总结
通过这个项目可以学习
- 软件开发流程
- Android布局文件设计
- SQLite数据库学习使用
- 公共类设计使用
- 在Android程序中操作SQLite数据库
生成apk文件可自行测试:https://pan.baidu.com/s/1_ZVCq_PRauPpjpQKh3HVHw
提取码:jm57