记账本
今日收支明细界面
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/grey_f3f3f3"> <RelativeLayout android:id="@+id/main_top_layout" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:text="@string/app_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:padding="10dp" android:textStyle="bold" android:textSize="18sp" android:textColor="@color/black"/> <ImageView android:id="@+id/main_iv_search" android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@mipmap/search" android:layout_alignParentRight="true" android:padding="10dp"/> </RelativeLayout> <ListView android:id="@+id/main_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/main_top_layout" android:padding="10dp" android:divider="@null" android:dividerHeight="6dp" android:scrollbars="none" android:background="@color/grey_f3f3f3"/> <ImageButton android:id="@+id/main_btn_more" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/more" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_margin="20dp" android:background="@drawable/main_morebtn_bg"/> <Button android:id="@+id/main_btn_edit" android:layout_width="100dp" android:layout_height="50dp" android:layout_alignBottom="@id/main_btn_more" android:background="@drawable/main_recordbtn_bg" android:layout_toLeftOf="@id/main_btn_more" android:text="@string/editone" android:textStyle="bold" android:textColor="@color/white" android:drawableLeft="@mipmap/edit" android:gravity="center_vertical"/> </RelativeLayout>
main_morebtn_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!-- 填充颜色--> <solid android:color="@color/green_006400"/> </shape>
main _recordbtn_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/green_006400"/> <!-- 表示四个角的弧度--> <corners android:radius="20dp"/> </shape>
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#008577</color> <color name="colorPrimaryDark">#00574B</color> <color name="colorAccent">#D81B60</color> <color name="black">#000000</color> <color name="grey_f3f3f3">#f3f3f3</color> <color name="grey_7D7D7D">#7D7D7D</color> <color name="white">#ffffff</color> <color name="green_006400">#006400</color> </resources>
strings.xml
<resources> <string name="app_name">简约记账</string> <string name="editone">记一笔...</string> <string name="month_out">本月支出</string> <string name="out">支出</string> <string name="month_in">本月收入</string> <string name="in">收入</string> <string name="budget">预算剩余</string> <string name="set_budget">设置预算</string> <string name="see_excel">查看图表分析</string> <string name="add_remark">添加备注</string> <string name="remark">备注</string> <string name="ensure">确定</string> <string name="cancel">取消</string> <string name="about">关于</string> <string name="setting">设置</string> <string name="history_record">账单记录</string> <string name="account_info">账单详情</string> <string name="please_input_time">请输入时间(24小时制)</string> <string name="please_search_info">请输入搜索信息</string> <string name="date_empty">数据为空,无此类记录~~</string> <string name="chart_info">账单详情</string> <string name="about_appinfo">应用信息</string> <string name="about_version">版本</string> <string name="about_1">1.0</string> <string name="history_info">本月账单记录</string> <string name="clear_all">清空所有记录</string> <!-- TODO: Remove or change this placeholder text --> <string name="not_data">暂无数据</string> </resources>
Mainactivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener { ListView todayLv; //展示今日收支情况的ListView ImageView searchIv; Button editBtn; ImageButton moreBtn; //声明数据源 List<AccountBean>mDatas; AccountAdapter adapter; int year,month,day; //头布局相关控件 View headerView; TextView topOutTv,topInTv,topbudgetTv,topConTv; ImageView topShowIv; 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的头布局 addLVHeaderView(); mDatas = new ArrayList<>(); //设置适配器:加载每一行数据到列表当中 adapter = new AccountAdapter(this, mDatas); todayLv.setAdapter(adapter); } /** 初始化自带的View的方法*/ private void initView() { todayLv = findViewById(R.id.main_lv); editBtn = findViewById(R.id.main_btn_edit); moreBtn = findViewById(R.id.main_btn_more); searchIv = findViewById(R.id.main_iv_search); editBtn.setOnClickListener(this); moreBtn.setOnClickListener(this); searchIv.setOnClickListener(this); setLVLongClickListener(); } /** 设置ListView的长按事件*/ 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); //获取正在被点击的这条信息 //弹出提示用户是否删除的对话框 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.deleteItemFromAccounttbById(click_id); mDatas.remove(clickBean); //实时刷新,移除集合当中的对象 adapter.notifyDataSetChanged(); //提示适配器更新数据 setTopTvShow(); //改变头布局TextView显示的内容 } }); builder.create().show(); //显示对话框 } /** 给ListView添加头布局的方法*/ private void addLVHeaderView() { //将布局转换成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_tv_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 incomeOneDay = DBManager.getSumMoneyOneDay(year, month, day, 1); float outcomeOneDay = DBManager.getSumMoneyOneDay(year, month, day, 0); String infoOneDay = "今日支出 ¥"+outcomeOneDay+" 收入 ¥"+incomeOneDay; topConTv.setText(infoOneDay); // 获取本月收入和支出总金额 float incomeOneMonth = DBManager.getSumMoneyOneMonth(year, month, 1); float outcomeOneMonth = DBManager.getSumMoneyOneMonth(year, month, 0); topInTv.setText("¥"+incomeOneMonth); topOutTv.setText("¥"+outcomeOneMonth); // 设置显示运算剩余 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 passwordMethod = PasswordTransformationMethod.getInstance(); topInTv.setTransformationMethod(passwordMethod); //设置隐藏 topOutTv.setTransformationMethod(passwordMethod); //设置隐藏 topbudgetTv.setTransformationMethod(passwordMethod); //设置隐藏 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; //设置标志位为隐藏状态 } } }

浙公网安备 33010602011771号