【安卓基础】基于Android Studio的家庭理财通项目

目录

声明:

此项目为移植《Android开发从入门到精通》项目实战篇,此项目仅用于学习。书本项目是基于Eclipse 4.2.2 + Android 5.0

开发运行环境

  • 操作系统:windows 10
  • JDK环境:java 8
  • 开发工具:Android Studio 4.2.2
  • 开发语言:java、XML、SQL
  • 数据库管理软件:SQLite 3

代码树

【安卓基础】基于Android Studio的家庭理财通项目
【安卓基础】基于Android Studio的家庭理财通项目

; 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))");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

FlayDAO.java


public class FlagDAO {
    private DBOpenHelper helper;
    private SQLiteDatabase db;

    public FlagDAO(Context context) {
        helper = new DBOpenHelper(context);
        db = helper.getWritableDatabase();
    }

    public void add(Tb_flag tb_flag) {
        db.execSQL("insert into tb_flag (_id,flag) values (?,?)", new Object[]{
                tb_flag.getid(), tb_flag.getFlag()});
    }

    public void update(Tb_flag tb_flag) {

        db.execSQL("update tb_flag set flag = ? where _id = ?", new Object[]{
                tb_flag.getFlag(), tb_flag.getid()});
    }

    @SuppressLint("Range")
    public Tb_flag find(int id) {

        Cursor cursor = db.rawQuery(
                "select _id,flag from tb_flag where _id = ?",
                new String[]{String.valueOf(id)});
        if (cursor.moveToNext()) {

            return new Tb_flag(cursor.getInt(cursor.getColumnIndex("_id")),
                    cursor.getString(cursor.getColumnIndex("flag")));
        }
        cursor.close();
        return null;
    }

    public void delete(Integer... ids) {
        if (ids.length > 0) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < ids.length; i++) {
                sb.append('?').append(',');
            }
            sb.deleteCharAt(sb.length() - 1);

            db.execSQL("delete from tb_flag where _id in (" + sb + ")",
                    (Object[]) ids);
        }
    }

    @SuppressLint("Range")
    public List<Tb_flag> getScrollData(int start, int count) {
        List<Tb_flag> lisTb_flags = new ArrayList<Tb_flag>();

        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;
    }

    public long getCount() {

        Cursor cursor = db.rawQuery("select count(_id) from tb_flag", null);
        if (cursor.moveToNext()) {
            return cursor.getLong(0);
        }
        cursor.close();
        return 0;
    }

    public int getMaxId() {

        Cursor cursor = db.rawQuery("select max(_id) from tb_flag", null);
        while (cursor.moveToLast()) {
            return cursor.getInt(0);
        }
        cursor.close();
        return 0;
    }
}

InAccountDAO.java


public class InAccountDAO {

    private DBOpenHelper helper;
    private SQLiteDatabase db;

    public InAccountDAO(Context context) {
        helper = new DBOpenHelper(context);
    }

    public void add(Tb_inAccount tb_inAccount) {
        db = helper.getWritableDatabase();

        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()
        });
    }

    public void update(Tb_inAccount tb_inAccount) {
        db = helper.getWritableDatabase();

        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()
        });
    }

    @SuppressLint("Range")
    public Tb_inAccount find(int id) {
        db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select _id,money,time,type,handler,mark from tb_inaccount where _id = ?", new String[]{
                String.valueOf(id)
        });
        if (cursor.moveToNext()) {

            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;
    }

    public Map<String, Float> getTotal() {
        List<Tb_inAccount> tb_inaccount = new ArrayList<Tb_inAccount>();
        db = helper.getWritableDatabase();

        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>();
        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;
    }

    public void delete(Integer... ids) {
        if (ids.length > 0) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < ids.length; i++) {
                sb.append('?').append(',');
            }
            sb.deleteCharAt(sb.length() - 1);
            db = helper.getWritableDatabase();

            db.execSQL("delete from tb_inaccount where _id in (" + sb + ")", (Object[]) ids);
        }
    }

    @SuppressLint("Range")
    public List<Tb_inAccount> getScrollData(int start, int count) {
        List<Tb_inAccount> tb_inAccounts = new ArrayList<>();
        db = helper.getWritableDatabase();
        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;
    }

    public long getCount() {
        db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select count(_id) from tb_inaccount", null);
        if (cursor.moveToNext()) {
            return cursor.getLong(0);
        }
        return 0;
    }

    public int getMaxId() {

        db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select max(_id) from tb_inaccount", null);
        while (cursor.moveToLast()) {
            return cursor.getInt(0);
        }
        return 0;
    }
}

OutAccountDAO.java


public class OutAccountDAO {
    private DBOpenHelper helper;
    private SQLiteDatabase db;

    public OutAccountDAO(Context context) {
        helper = new DBOpenHelper(context);
        db = helper.getWritableDatabase();
    }

    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()});
    }

    public void update(Tb_outaccount tb_outaccount) {

        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()});
    }

    @SuppressLint("Range")
    public Tb_outaccount find(int id) {

        Cursor cursor = db
                .rawQuery(
                        "select _id,money,time,type,address,mark from tb_outaccount where _id = ?",
                        new String[]{String.valueOf(id)});
        if (cursor.moveToNext()) {

            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;
    }

    public void delete(Integer... ids) {
        if (ids.length > 0) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < ids.length; i++) {
                sb.append('?').append(',');
            }
            sb.deleteCharAt(sb.length() - 1);

            db.execSQL("delete from tb_outaccount where _id in (" + sb + ")",
                    (Object[]) ids);
        }
    }

    public Map<String, Float> getTotal() {

        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>();
        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;
    }

    @SuppressLint("Range")
    public List<Tb_outaccount> getScrollData(int start, int count) {
        List<Tb_outaccount> tb_outaccount = new ArrayList<Tb_outaccount>();

        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;
    }

    public long getCount() {

        Cursor cursor = db.rawQuery("select count(_id) from tb_outaccount",
                null);
        if (cursor.moveToNext()) {
            return cursor.getLong(0);
        }
        cursor.close();
        return 0;
    }

    public int getMaxId() {

        Cursor cursor = db.rawQuery("select max(_id) from tb_outaccount", null);
        while (cursor.moveToLast()) {
            return cursor.getInt(0);
        }
        cursor.close();
        return 0;
    }
}

PwdDAO.java


public class PwdDAO {
    private DBOpenHelper helper;
    private SQLiteDatabase db;

    public PwdDAO(Context context) {
        helper = new DBOpenHelper(context);
        db = helper.getWritableDatabase();
    }

    public void add(Tb_pwd tb_pwd) {

        db.execSQL("insert into tb_pwd (password) values (?)",
                new Object[]{tb_pwd.getPassword()});
    }

    public void update(Tb_pwd tb_pwd) {

        db.execSQL("update tb_pwd set password = ?",
                new Object[]{tb_pwd.getPassword()});
    }

    @SuppressLint("Range")
    public Tb_pwd find() {

        Cursor cursor = db.rawQuery("select password from tb_pwd", null);
        if (cursor.moveToNext()) {

            return new Tb_pwd(cursor.getString(cursor
                    .getColumnIndex("password")));
        }
        cursor.close();
        return null;
    }

    public long getCount() {

        Cursor cursor = db.rawQuery("select count(password) from tb_pwd", null);
        if (cursor.moveToNext()) {
            return cursor.getLong(0);
        }
        cursor.close();
        return 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

显示界面

【安卓基础】基于Android Studio的家庭理财通项目

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);
                    startActivity(intent);
                    break;
                case 3:
                    intent = new Intent(MainActivity.this, InAccountInfo.class);
                    startActivity(intent);
                    break;
                case 4:
                    intent = new Intent(MainActivity.this, Showinfo.class);
                    startActivity(intent);
                    break;
                case 5:
                    intent = new Intent(MainActivity.this, Sysset.class);
                    startActivity(intent);
                    break;
                case 6:
                    intent = new Intent(MainActivity.this, Accountflag.class);
                    startActivity(intent);
                    break;
                case 7:
                    intent = new Intent(MainActivity.this, Help.class);
                    startActivity(intent);
                    break;
                case 8:
                    finish();
                    System.exit(0);

            }
        });

    }

    static class pictureAdapter extends BaseAdapter {
        private final LayoutInflater inflater;
        private final List<Picture> pictures;

        public pictureAdapter(String[] titles, int[] images, Context context) {
            super();
            pictures = new ArrayList<>();
            inflater = LayoutInflater.from(context);
            for (int i = 0; i < images.length; i++){
                Picture picture = new Picture(titles[i], images[i]);
                pictures.add(picture);
            }
        }

        @Override
        public int getCount() {
            if (null != pictures) {
                return pictures.size();
            } else {
                return 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;
            if (arg1 == null){
                arg1 = inflater.inflate(R.layout.gvitem, null);
                viewHolder = new 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{
        public TextView title;
        public ImageView image;
    }

    static class Picture {
        private String title;
        private int imageId;

        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

【安卓基础】基于Android Studio的家庭理财通项目

public class AddOutAccount extends Activity {
    protected static final int DATE_DIALOG_ID = 0;
    EditText txtMoney, txtTime, txtAddress, txtMark;
    Spinner spType;
    Button btnSaveButton;
    Button btnCancelButton;

    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 -> {

            showDialog(DATE_DIALOG_ID);
        });

        btnSaveButton.setOnClickListener(arg0 -> {

            String strMoney = txtMoney.getText().toString();
            if (!strMoney.isEmpty()) {

                OutAccountDAO outaccountDAO = new OutAccountDAO(
                        AddOutAccount.this);

                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 -> {

            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;
        switch (id) {
            case DATE_DIALOG_ID:// 弹出日期选择对话框
                return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                        mDay);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        @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;
    Spinner spInType;
    Button btnInSaveButton;
    Button btnInCancelButton;

    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 = new InAccountDAO(
                        AddInAccount.this);

                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){
        switch (id) {
            case DATE_DIALOG_ID:// 弹出日期选择对话框
                return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                        mDay);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                @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

【安卓基础】基于Android Studio的家庭理财通项目

public class OutAccountInfo extends Activity {
    public static final String FLAG = "id";
    ListView lvinfo;
    String strType = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.outaccountinfo);
        lvinfo = (ListView) findViewById(R.id.lvoutaccountinfo);

        ShowInfo(R.id.btnoutinfo);

        lvinfo.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @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.putExtra(FLAG, new String[] { strid, strType });
                startActivity(intent);
            }
        });
    }

    private void ShowInfo(int intType) {
        String[] strInfos;
        ArrayAdapter<String> arrayAdapter;
        strType = "btnoutinfo";
        OutAccountDAO outaccountinfo = new OutAccountDAO(OutAccountInfo.this);

        List<Tb_outAccount> listoutinfos = outaccountinfo.getScrollData(0,
                (int) outaccountinfo.getCount());
        strInfos = new String[listoutinfos.size()];
        int i = 0;
        for (Tb_outAccount tb_outaccount : listoutinfos) {

            strInfos[i] = tb_outaccount.getid() + "|" + tb_outaccount.getType()
                    + " " + String.valueOf(tb_outaccount.getMoney()) + "元     "
                    + tb_outaccount.getTime();
            i++;
        }

        arrayAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, strInfos);
        lvinfo.setAdapter(arrayAdapter);
    }

    @Override
    protected void onRestart() {

        super.onRestart();
        ShowInfo(R.id.btnoutinfo);
    }
}

InAccountInfo.java


public class InAccountInfo extends Activity {
    public static final String FLAG = "id";
    ListView lvinfo;
    String strType = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.inaccountinfo);
        lvinfo = (ListView) findViewById(R.id.lvinaccountinfo);

        ShowInfo(R.id.btnininfo);

        lvinfo.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @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.putExtra(FLAG, new String[] { strid, strType });
                startActivity(intent);
            }
        });
    }

    private void ShowInfo(int intType) {
        String[] strInfos = null;
        ArrayAdapter<String> arrayAdapter = null;
        strType = "btnininfo";
        InAccountDAO inaccountinfo = new InAccountDAO(InAccountInfo.this);

        List<Tb_inAccount> listinfos = inaccountinfo.getScrollData(0,
                (int) inaccountinfo.getCount());
        strInfos = new String[listinfos.size()];
        int m = 0;
        for (Tb_inAccount tb_inaccount : listinfos) {

            strInfos[m] = tb_inaccount.get_id() + "|" + tb_inaccount.getType()
                    + " " + String.valueOf(tb_inaccount.getMoney()) + "元     "
                    + tb_inaccount.getTime();
            m++;
        }

        arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, strInfos);
        lvinfo.setAdapter(arrayAdapter);
    }

    @Override
    protected void onRestart() {

        super.onRestart();
        ShowInfo(R.id.btnininfo);
    }
}

ShowInfo.java

【安卓基础】基于Android Studio的家庭理财通项目

public class Showinfo extends Activity {
    public static final String FLAG = "id";
    Button btnoutinfo, btnininfo, btnflaginfo;
    ListView lvinfo;
    String strType = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.showinfo);

        lvinfo = findViewById(R.id.lvinfo);
        btnoutinfo = findViewById(R.id.btnoutinfo);
        btnininfo = findViewById(R.id.btnininfo);
        btnflaginfo = findViewById(R.id.btnflaginfo);

        btnoutinfo.setOnClickListener(arg0 -> {

            ShowInfo(R.id.btnoutinfo);
        });

        btnininfo.setOnClickListener(arg0 -> {

            ShowInfo(R.id.btnininfo);
        });

        btnflaginfo.setOnClickListener(arg0 -> {

            ShowInfo(R.id.btnflaginfo);
        });

        lvinfo.setOnItemClickListener((parent, view, position, id) -> {
            String strInfo = String.valueOf(((TextView) view).getText());
            String strid = strInfo.substring(0, strInfo.indexOf('|'));
            Intent intent = null;
            if (strType == "btnflaginfo") {
                intent = new Intent(Showinfo.this, FlagManage.class);
                intent.putExtra(FLAG, strid);
                startActivity(intent);
            }
        });
    }

    private void ShowInfo(int intType) {
        String[] strInfos = null;
        ArrayAdapter<String> arrayAdapter;
        Intent intent = null;
        switch (intType) {
            case R.id.btnoutinfo:// 如果是支出按钮btnoutinfo
                strType = "outinfo";
                intent = new Intent(Showinfo.this, TotalChart.class);
                intent.putExtra("passType", strType);
                startActivity(intent);
                break;
            case R.id.btnininfo:// 如果是收入按钮btnininfo

                strType = "ininfo";
                intent = new Intent(Showinfo.this, TotalChart.class);
                intent.putExtra("passType", strType);
                startActivity(intent);
                break;
            case R.id.btnflaginfo:// 如果是btnflaginfo按钮
                strType = "btnflaginfo";
                FlagDAO flaginfo = new FlagDAO(Showinfo.this);

                List<Tb_flag> listFlags = flaginfo.getScrollData(0,
                        (int) flaginfo.getCount());
                strInfos = new String[listFlags.size()];
                int n = 0;
                for (Tb_flag tb_flag : listFlags) {

                    strInfos[n] = tb_flag.getid() + "|" + tb_flag.getFlag();
                    if (strInfos[n].length() > 30)
                        strInfos[n] = strInfos[n].substring(0, 30) + "......";
                    n++;
                }

                arrayAdapter = new ArrayAdapter<>(this,
                        android.R.layout.simple_list_item_1, strInfos);
                lvinfo.setAdapter(arrayAdapter);
                break;
        }
    }

}

Sysset.java


public class Sysset extends Activity {
    EditText txtpwd;
    Button btnSet, btnsetCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        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) {

                PwdDAO pwdDAO = new PwdDAO(Sysset.this);
                Tb_pwd tb_pwd = new Tb_pwd(txtpwd.getText().toString());
                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) {

                txtpwd.setText("");
                txtpwd.setHint("请输入密码");
            }
        });
    }
}

AccountFlag.java


public class AccountFlag extends Activity {

    EditText txtFlag;
    Button btnflagSaveButton;
    Button btnflagCancelButton;

    @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);
                Tb_flag tb_flag = new Tb_flag(
                        flagDAO.getMaxId() + 1, strFlag);
                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

【安卓基础】基于Android Studio的家庭理财通项目
public class Help extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help);
        WebView webview= findViewById(R.id.webView1);

        StringBuilder sb=new StringBuilder();
        sb.append("《家庭理财通》使用帮助:");
        sb.append("");
        sb.append("修改密码:选择"系统设置"模块可以修改登录密码,项目运行时,默认没有密码。");
        sb.append("支出管理:选择"新增支出"模块可以添加支出信息;选择"我的支出"模块可以查看、修改或删除支出信息。");
        sb.append("收入管理:选择"新增收入"模块可以添加收入信息;选择"我的收入"模块可以查看、修改或删除收入信息。");
        sb.append("便签管理:选择"收支便签"模块可以添加便签信息;选择"数据管理"模块中的"便签信息"按钮可以查看、修改或删除便签信息。");
        sb.append("退出系统:选择"退出"模块可以退出《家庭理财通》项目。");
        sb.append("");
        webview.loadDataWithBaseURL(null, sb.toString(),"text/html","utf-8",null);
    }
}

数据库管理

查看数据库

View->Tool Windows->Database Inspector

【安卓基础】基于Android Studio的家庭理财通项目

; 总结

通过这个项目可以学习

  • 软件开发流程
  • Android布局文件设计
  • SQLite数据库学习使用
  • 公共类设计使用
  • 在Android程序中操作SQLite数据库

生成apk文件可自行测试:https://pan.baidu.com/s/1_ZVCq_PRauPpjpQKh3HVHw
提取码:jm57

Original: https://blog.csdn.net/keep_today/article/details/122596898
Author: @一尘√
Title: 【安卓基础】基于Android Studio的家庭理财通项目

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/815876/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球