首页 > 技术文章 > 个人作业家庭记账本

wfswf 2021-06-20 21:14 原文

家庭记账本

这个项目是我完成的一个比较正规的android项目,本来老师是让假期在家里做的,结果我因有点事,到了后来才补上。当然这个项目也是我一行一行的跟着视频敲下来的项目。这个项目让我了解了adapter也解除了自定义软键盘书写以及一些接口调用的功能。

                                    

代码展示

package com.example.jishiben;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.jishiben.adapter.AccountAdapter;
import com.example.jishiben.db.AccountBean;
import com.example.jishiben.db.DBManager;
import com.example.jishiben.utils.BudgetDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ListView todayLv;//展示今日收支情况的ListView
    //声明数据源
    List<AccountBean> mDatas;
    AccountAdapter adapter;
    int year,month,day;
    //头布局相关控件
    View headerView;
    TextView topOutTv,topInTv,topBudgetTv,topConTv;
    ImageView topShowIv;
    private List<AccountBean> accountListOneDayFromAccounttb;

    ImageView searchIv;
    ImageButton moreBtn;
    Button editBtn;
    SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initTime();
        initView();
        preferences = getSharedPreferences("budget", Context.MODE_PRIVATE);
        //添加ListView的头布局
        addLVHeadView();
        mDatas = new ArrayList<>();
        //设置适配器加载每一行数据列表
        adapter = new AccountAdapter(this, mDatas);
        todayLv.setAdapter(adapter);
    }

    private void initView() {
        todayLv = findViewById(R.id.main_lv);
        searchIv = findViewById(R.id.main_iv_search);
        editBtn = findViewById(R.id.main_btn_edit);
        moreBtn = findViewById(R.id.main_btn_more);
        searchIv.setOnClickListener(this);
        editBtn.setOnClickListener(this);
        moreBtn.setOnClickListener(this);
        setLVLongClickListener();
    }
    //长按删除某条记录
    private void setLVLongClickListener() {
        todayLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==0){//点击了头布局
                    return false;
                }
                int pos = position-1;
                AccountBean clickBean = mDatas.get(pos);//获取正在被点击的这条信息
               // int click_id = clickBean.getId();
                //弹出提示用户是否删除的对话框
                showDeleteItemDialog(clickBean);
                return false;
            }
        });
    }

    private void showDeleteItemDialog(final AccountBean clickBean) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息").setMessage("您确定删除这条记录吗?")
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int click_id = clickBean.getId();
                        //执行被删除的操作
                        DBManager.deleteFromAccounttbById(click_id);
                        mDatas.remove(clickBean);//实时刷新,在listView中移除被删除的对象
                        adapter.notifyDataSetChanged();//提示适配器刷新
                        setTopTvShow();//改变头布局显示的内容
                    }
                });
        builder.create().show();//显示对话框

    }

    //listview添加头布局的方法
    private void addLVHeadView() {
        //将布局转化成view对象
        headerView = getLayoutInflater().inflate(R.layout.item_mainlv_top,null);
        todayLv.addHeaderView(headerView);
        //查找头布局可用控件
        topOutTv = headerView.findViewById(R.id.item_mainlv_top_tv_out);
        topInTv = headerView.findViewById(R.id.item_mainlv_top_tv_in);
        topBudgetTv = headerView.findViewById(R.id.item_mainlv_top_tv_budget);
        topConTv = headerView.findViewById(R.id.item_mainlv_top_day);
        topShowIv = headerView.findViewById(R.id.item_mainlv_top_iv_hide);

        topBudgetTv.setOnClickListener(this);
        headerView.setOnClickListener(this);
        topShowIv.setOnClickListener(this);


    }
    //获取今日的具体时间
    private void initTime(){
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH)+1;
        day = calendar.get(Calendar.DAY_OF_MONTH);
    }

    //Activity获取焦点时调用的方法
    @Override
    protected void onResume() {
        super.onResume();
        loadDBData();
        setTopTvShow();
    }
    //设置头布局当中的文本显示
    private void setTopTvShow() {
        //获取今日支出和收入的总金额,显示在view
        float intcomeOneDay = DBManager.getSumMoneyOneDay(year,month,day,1);
        float outcomeOneDay = DBManager.getSumMoneyOneDay(year,month,day,0);
        String infoOneDay = "今日支出 "+outcomeOneDay+",今日收入¥"+intcomeOneDay;
        topConTv.setText(infoOneDay);
        //获取本月收入和支出金额
        float incomeOneMonth = DBManager.getSumMoneyOneMonth(year,month,1);
        float outcomeOneMonth = DBManager.getSumMoneyOneMonth(year,month,0);
        topInTv.setText(""+incomeOneMonth);
        topOutTv.setText(""+outcomeOneMonth);

        //设置显示剩余money
        float bmoney = preferences.getFloat("bmoney",0);//预算
        if(bmoney ==0){
            topBudgetTv.setText(" 0");
        }else{
            float syMoney = bmoney-outcomeOneMonth;
            topBudgetTv.setText(""+syMoney);
        }
    }



    private void loadDBData() {
        List<AccountBean> list = DBManager.getAccountListOneDayFromAccounttb(year, month, day);
        mDatas.clear();
        mDatas.addAll(list);
        adapter.notifyDataSetChanged();
    }

    //布局相关的点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_iv_search:
                Intent it = new Intent(this,SearchActivity.class);
                startActivity(it);
                break;
            case R.id.main_btn_edit:
                Intent it1 = new Intent(this,RecordActivity.class);
                startActivity(it1);
                break;
            case R.id.main_btn_more:
                MoreDialog moreDialog = new MoreDialog(this);
                moreDialog.show();
                moreDialog.setDialogSize();
                break;
            case R.id.item_mainlv_top_tv_budget:
                showBudgetDialog();
                break;
            case R.id.item_mainlv_top_iv_hide:
                //切换textview明文和密文
                toggleShow();
                break;
        }
        if(v==headerView){
            //头布局被点击了
            Intent intent = new Intent();
            intent.setClass(this,MonthChartActivity.class);
            startActivity(intent);
        }
    }
    //显示运算设置对话框
    private void showBudgetDialog() {
        BudgetDialog dialog = new BudgetDialog(this);
        dialog.show();
        dialog.setDialogSize();
        dialog.setOnEnsureListener(new BudgetDialog.OnEnsureListener() {
            @Override
            public void onEnsure(float money) {
                //将预算金额写入到共享参数中进行存储
                SharedPreferences.Editor editor = preferences.edit();
                editor.putFloat("bmoney",money);
                editor.commit();


                //计算剩余金额
                float outcomeOneMonth = DBManager.getSumMoneyOneMonth(year,month,0);
                float syMoney = money-outcomeOneMonth;
                topBudgetTv.setText(""+syMoney);
            }
        });

    }

    boolean isShow = true;
    //点击头部眼睛是如果原来是明文就加密,密文就显示
    private void toggleShow() {
        if(isShow){//明文--->密文
            PasswordTransformationMethod passwordTransformationMethod = PasswordTransformationMethod.getInstance();
            topInTv.setTransformationMethod(passwordTransformationMethod);
            topOutTv.setTransformationMethod(passwordTransformationMethod);
            topBudgetTv.setTransformationMethod(passwordTransformationMethod);
            topShowIv.setImageResource(R.mipmap.ih_hide);
            isShow = false;//设置标志位为隐藏状态
        }else {//密文---->明文
            HideReturnsTransformationMethod hideMethod = HideReturnsTransformationMethod.getInstance();
            topInTv.setTransformationMethod(hideMethod);
            topOutTv.setTransformationMethod(hideMethod);
            topBudgetTv.setTransformationMethod(hideMethod);
            topShowIv.setImageResource(R.mipmap.ih_show);
            isShow = true;
        }
    }
}

 

推荐阅读