家庭记账本(六)主界面头布局
将头布局在ListView中显示,也就是在ListView中添加一个头
/*添加ListView头布局*/ public void addListViewHeader() { headerView = getLayoutInflater().inflate(R.layout.item_mainlv_top, null); lv_today_info.addHeaderView(headerView); /*查找可用控件*/ tv_top_out = findViewById(R.id.item_mainlv_top_out); tv_top_in = findViewById(R.id.item_mainlv_top_tv_in); tv_top_budget = findViewById(R.id.item_mainlv_top_tv_budget); tv_top_condition = findViewById(R.id.item_mainlv_top_tv_day); iv_show = findViewById(R.id.item_mainlv_top_iv_hide); headerView.setOnClickListener(this); iv_show.setOnClickListener(this); tv_top_budget.setOnClickListener(this); }
那么头布局中的信息如何显示呢?为了能获取头部中的信息,我们需要填写两个数据库中的信息。
/*获取某一天支出或者收入的总金额*/ public static float getSumMoneyOneDay(int year, int month, int day, int kind) { float total = 0; String sql = "select sum(money) from tb_account where year=? and month=? and day=? and kind=?"; Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(year), String.valueOf(month), String.valueOf(day), String.valueOf(kind)}); if (cursor.moveToFirst()) { total = cursor.getFloat(cursor.getColumnIndex("sum(money)")); } return total; } /*获取某一月支出或者收入的总金额*/ public static float getSumMoneyOneMonth(int year, int month, int kind) { float total = 0; String sql = "select sum(money) from tb_account where year=? and month=? and kind=?"; Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(year), String.valueOf(month), String.valueOf(kind)}); if (cursor.moveToFirst()) { total = cursor.getFloat(cursor.getColumnIndex("sum(money)")); } return total; }
将数据库中的信息显示出来
/*设置顶部布局文本内容的显示*/ public void setTopTVShow(){ /*获取今日支出和收入总金额*/ float income = DBManager.getSumMoneyOneDay(year, month, day, 1); float outcome = DBManager.getSumMoneyOneDay(year, month, day, 0); String infoOneday = "今日支出 ¥"+outcome+" 收入 ¥"+income; tv_top_condition.setText(infoOneday); /*获取本月支出和收入总金额*/ float income2 = DBManager.getSumMoneyOneMonth(year, month, 1); float outcome2 = DBManager.getSumMoneyOneMonth(year, month, 0); tv_top_in.setText("+"+income2); tv_top_out.setText("-"+outcome2); /*设置显示预算剩余*/ float budget = preferences.getFloat("bmoney", 0); tv_top_budget.setText(String.valueOf((budget-outcome2))); }
我们如果希望可以设置预算就需要另写一个预算的对话框,写法和时间对话框基本一致。
我们在之前看到ListView的头页面有一个眼睛,希望实现明文与密文的切换。
boolean isShow = true; public void toggleShow() { if (isShow) { PasswordTransformationMethod instance1 = PasswordTransformationMethod.getInstance(); tv_top_in.setTransformationMethod(instance1); tv_top_out.setTransformationMethod(instance1); tv_top_budget.setTransformationMethod(instance1); iv_show.setImageResource(R.mipmap.ic_hide); isShow = false; } else { HideReturnsTransformationMethod instance2 = HideReturnsTransformationMethod.getInstance(); tv_top_in.setTransformationMethod(instance2); tv_top_out.setTransformationMethod(instance2); tv_top_budget.setTransformationMethod(instance2); iv_show.setImageResource(R.mipmap.ic_show); isShow = true; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南