首页 > 其他分享 >2.19 Android 练习

2.19 Android 练习

时间:2024-02-28 14:23:42浏览次数:20  
标签:headerView mainlv void 练习 findViewById 2.19 import Android id

package com.zhen.accountbook;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.zhen.accountbook.adapter.AccountAdapter;
import com.zhen.accountbook.db.AccountBean;
import com.zhen.accountbook.db.DBManager;
import com.zhen.accountbook.utils.BeiZhuDialog;
import com.zhen.accountbook.utils.BudgetDialog;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ListView todayLv;//展示今日收支情况
ImageView searchIv;
Button editBtn;
ImageButton moreBtn;
//声明数据源
List<AccountBean> mDatas;
AccountAdapter adapter;
//头布局相关的控件
TextView topOutTv, topInTv, topbudgetTv, topConTv;
ImageView topShowIv;

int year, month, day;
View headerView;
SharedPreferences preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTime();
extracted();
preferences = getSharedPreferences("budget", Context.MODE_PRIVATE);
addLVHeaderView();
mDatas = new ArrayList<>();
//设置适配器:加载每一行数据到列表当中
adapter = new AccountAdapter(this, mDatas);
todayLv.setAdapter(adapter);
}

private void extracted() {

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

private void addLVHeaderView() {
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() {
float outMonthCome = DBManager.getMoneyOneMonth(year, month, 0);
float inMonthCome = DBManager.getMoneyOneMonth(year, month, 1);
float outDayCome = DBManager.getMoneyOneDay(year, month, day, 0);
float inDayCome = DBManager.getMoneyOneDay(year, month, day, 1);
String infoOneDay = "今日支出 ¥" + outDayCome + " ,收入 ¥" + inDayCome;
topConTv.setText(infoOneDay);
topInTv.setText("¥ " + inMonthCome);
topOutTv.setText("¥ " + outMonthCome);
//设置预算剩余
float bMoney = preferences.getFloat("bmoney", 0);
if (bMoney == 0) {
topbudgetTv.setText(" ¥0");
} else {
float bMoney1 = bMoney - outMonthCome;
topbudgetTv.setText(" ¥" + bMoney1);
}

}

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

//button相关的点击事件

@Override
public void onClick(View view) {
if (view.getId() == R.id.main_iv_search) {
} else if (view.getId() == R.id.main_btn_edit) {
Intent it1 = new Intent(this, RecordActivity.class); //跳转界面
startActivity(it1);
} else if (view.getId() == R.id.main_btn_more) {
} else if (view.getId() == R.id.item_mainlv_top_tv_budget) {
showBudgetDialog();
} else if (view == headerView) {
//头布局被点击

} else if (view.getId() == R.id.item_mainlv_top_iv_hide) {
//切换明文和密文
toggleShow();
}
}

private void showBudgetDialog() {
//显示设置预算对话框
BudgetDialog dialog = new BudgetDialog(this);
dialog.show();
dialog.setOnEnsureListener(money -> {
//将金额写入共享参数中,进行存储
SharedPreferences.Editor editor = preferences.edit();
editor.putFloat("bmoney", money);
editor.commit();
//计算剩余金额
float outComeMonth = DBManager.getMoneyOneMonth(year, month, 0);
float sMoney = money - outComeMonth;//预算剩余
topbudgetTv.setText("¥ " + sMoney);
});
}

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 hideReturnsTransformationMethod = HideReturnsTransformationMethod.getInstance();
topInTv.setTransformationMethod(hideReturnsTransformationMethod);
topOutTv.setTransformationMethod(hideReturnsTransformationMethod);
topbudgetTv.setTransformationMethod(hideReturnsTransformationMethod);
topShowIv.setImageResource(R.mipmap.ih_show);
isShow = true;
}
}
}

标签:headerView,mainlv,void,练习,findViewById,2.19,import,Android,id
From: https://www.cnblogs.com/dmx-03/p/18040259

相关文章

  • 已有Android项目接入有方yfb101错误,应用不停自动重启
    最近在接入有方信息的yfb101签字板,在按照demo导入所有数据和信息之后,却发现无法打开指纹设备,一直报错usbpermission没有。经过反复对比和新建项目进行比较,发现是因为cpu架构问题,因为有方的和之前的架构不一样,之前的在app/build.gradle下面限定了ndk{abiF......
  • 级课堂测试试卷—数据同步练习
    石家庄铁道大学2024年春季  2022级课堂测试试卷—数据同步练习课程名称: 大数据库技术与应用  任课教师:王建民  考试时间: 120分钟  一、 数据结构分析:(1)京津冀三省的2015年度的科技成果数据原始表,为Access数据库,; (2)要求将三省的科技成果数据汇总到同一表中(要......
  • dp练习
    合并类len=2,[k]消消乐类,len=3,[i+1][j-1]else[k]Bracketshttps://vjudge.net/problem/POJ-2955#include<iostream>#include<cstring>usingnamespacestd;intdp[101][101];boolflag=1;boolpei(inti,intj,chara[]){if(a[i]=='('&&......
  • 2024.02.19 测试
    BeforewritingAlltheproblemsin2024.02.18测试and2024.02.19测试inhere:linkT1素数Linkgxyzoj:#3598素数Luogu:UVA1210连续素数之和UVa:1210-SumofConsecutivePrimeNumbersDescriptionSomepositiveintegerscanberepresentedbyasumofo......
  • ctfshow sql注入练习记录
    前言:继续做ctfshow的题了,本次为sql注入专题,从ctfshowweb177开始ctfshowweb177对空格,--+进行了过滤,注释符要换成#的url编码%23使用万能密码可以绕过1'or'1'='1';%23也可也使用/**/对空格进行绕过,进行联合查询-1'union/**/select/**/1,2,password/**/from/**/ctfshow_use......
  • android 混淆规则作用,Android代码混淆详解
    一、混淆的意义混淆代码并不是让代码无法被反编译,而是将代码中的类、方法、变量等信息进行重命名,把它们改成一些毫无意义的名字,同时也可以移除未被使用的类、方法、变量等。所以直观的看,通过混淆可以提高程序的安全性,增加逆向工程的难度,同时也有效缩减了apk的体积。总结如下:1、......
  • Android权限警告(not in privapp-permissions whitelist)
    1.现象模块使用了Settings.Global之后,单编模块push到手机里面重启,发现手机卡在开机logo界面,开不了机2.抓取logcat看log打印会发现如下图片中的打印,主要的关键词为Privilegedpermissionsnotinprivapp-permissionswhitelist二.查找源码定位问题(Q的代码)文件路径PermissionM......
  • Android Compose开发
    目录好处入门Composable布局其他组件列表verticalScroll延迟列表内容内边距性能修饰符偏移量requiredSize滚动添加间距SpacerButtonContext文字图片TextField重组状态提升viewmodel互相调用AndroidView项目学习其他text加一个背景paddingzIndexLaunchedEffectDisposableEffectpa......
  • Android 《设置全屏隐藏状态栏》
    @OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);//全屏去状态栏(在setContentView之前)requestWindowFeature(Window.FEATURE_NO_TITLE);getWindow().addFlags(WindowManager.La......
  • android - Kivy - 更改 FileChooser 默认位置
    fragment类(class):pangufeitianmeng,BFEBFBFF00040651W621LVLVpangufeitianmeng,BFEBFBFF000806C1E823_8FApangufeitianmeng,BFEBFBFF000806C26479_A74pangufeitianmeng,BFEBFBFF000306C3S2SMJ9CD,classLoadDialog(FloatLayout):load=ObjectProperty(None)cancel=......