首页 > 其他分享 >6.17 2

6.17 2

时间:2024-06-17 23:56:23浏览次数:28  
标签:headerView mainlv void 6.17 findViewById import id

记账本APP

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,6.17,findViewById,import,id
From: https://www.cnblogs.com/zzqq1314/p/18253465

相关文章

  • 6.17 5
    自定义软键盘packagecom.zhen.accountbook.utils;importandroid.inputmethodservice.Keyboard;importandroid.inputmethodservice.KeyboardView;importandroid.text.Editable;importandroid.text.InputType;importandroid.view.View;importandroid.widget.EditTex......
  • 6.17 7
    packagecom.zhen.accountbook.frag_record;importandroid.inputmethodservice.KeyboardView;importandroid.os.Bundle;importandroid.text.TextUtils;importandroid.widget.*;importandroidx.annotation.Nullable;importandroidx.fragment.app.Fragment;import......
  • 6.17 6
    packagecom.zhen.accountbook.utils;importandroid.app.Dialog;importandroid.content.Context;importandroid.os.Bundle;importandroid.text.TextUtils;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid......
  • 6.17 8
    packagecom.zhen.accountbook.adapter;importandroid.content.Context;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.ImageView;importandroid.widget.T......
  • 6.17 10
    <?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layou......
  • 6.17 9
    <?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="wrap_con......
  • 2024.6.17鲜花/错误的号码
    XY星的星际新闻报一直不太畅销,所以报纸上会有一些广告,毕竟星际新闻局的非机器人员工也得吃饭。有一则广告是这样的:【数据删除】研学基地位于【数据删除】,该研学基地致力于让学生体验一个幻想纪前的生活并培养学生不借助现代高科技的群居生活能力。该研学基地将于幻想历元年六......
  • 6.17 学习心得
    这本书讲述了几十年前软件专案管理问题与经验,作者将大型系统开发比作一个焦油坑,我原本以为软件开发还是比较容易的,有了新想法,就会有新的软件产品出现,但是却不知道项目不能满足目标、进度、预算的要求,就不能成为一个好项目。程序,通过不同的途径转变成不同的产物,使之变得更有用,成本......
  • dart最新2024.06.17
    import'package:flutter/material.dart';voidmain(){runApp(constMyApp());}classMyAppextendsStatelessWidget{constMyApp({super.key});@overrideWidgetbuild(BuildContextcontext){returnconstMaterialApp(title:&......
  • 云原生周刊:Harbor v2.11 版本发布 | 2024.6.17
    开源项目推荐DeschedulerDescheduler是一个工具,可用于优化Kubernetes集群中Pod的部署位置。它可以找到可以移动的Pod,并将其驱逐,让默认调度器将它们重新调度到更合适的节点上。ProwlerProwler是一款适用于AWS、Azure、GCP和Kubernetes的开源安全工具,用于进行安全评......