ToB企服应用市场:ToB评测及商务社交产业平台
标题:
Android Studio 实现小熊记账本 (功能多,简单)
[打印本页]
作者:
涛声依旧在
时间:
2024-6-15 02:25
标题:
Android Studio 实现小熊记账本 (功能多,简单)
一、高质量源码(非开源,白嫖低价勿扰)
关注公众号:《编程乐学》
后台复兴:
24010601
同款低价传送链接(仅9.9):
网络资源模板--基于 Android Studio 实现的简约记账本-CSDN博客
目次
前言
一、运行演示
二、开发环境
三、完成步骤
步骤 1:创建项目
步骤 2:创建包名
步骤 3:实现启动页
步骤 5:实现用户注册
步骤 6:实现用户登录
步骤 7:实现主页面编写
步骤 8:记账页面编写
步骤 9:历史记录页面编写
步骤10:我的页面的编写
步骤11:账单搜刮页面的编写
步骤12:统计页面的编写
四、获取源码
前言
本次实现的Android Studio 小熊记账本 (功能多,简单)项目,核心功能是可以记录收支情况,可以给收支选择各种分类选项,好比“餐饮”、“零食”、“购物”等等,还可以添加备注和修改日期。然后会在首页展示你的本月支持和收入信息,还可以选择隐蔽信息掩护隐私。通过【账单记录】可以查察自己的所有账单记录,通过【账单详情】可以看到用柱状图显示的账单情况,在设置内里可以清空所有数据,长按可以删除单条数据,功能非常全面丰富。
一、运行演示
我们先来看下运行演示结果
Android Studio 实现小熊记账本App
二、开发环境
我的开发环境如下,各人的AS版本不必要和我相同,只要是近两年从官网下载的版本,都是比4.0.0 (2020)高的,是可以满意运行和开发要求的。
三、完成步骤
步骤 1:创建项目
打开 Android studio 开发工具后,进行项目创建,左上角 File—>New Project、填写后点击 Finish 完成创建!
步骤 2:创建包名
选中com.example.note包 名 右 键 New — >package 并按需求依次 activity(存放各类 Activity)、adapter(存放各类适配器) 等包名,后续代码将按对应包名去创建,并将 MainActivity.java 移动到 activity包 下
步骤 3:实现启动页
在 activity包上右键创建 New—>Activity—>Empty Activity 选项创建 Activity 后弹 出对话框,输入相关信息,即可创建 Activity.
启动页页面结构配景放置一张自己喜好的logo即可
这里我们直接看java部分代码:
package com.example.tallybook.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import com.example.tallybook.R;
public class StartActivity extends AppCompatActivity {
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
@Override
public void run() {
tomainActive();
}
};
// 进入主页面
private void tomainActive() {
startActivity(new Intent(this, LoginActivity.class));
// 跳转完成后注销
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
}
复制代码
步骤 5:实现用户注册
在 activity包上右键创建 New—>Activity—>Empty Activity 选项创建 Activity 后弹 出对话框,输入相关信息,即可创建 Activity.
用户注册页面结构代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c3def0"
tools:context=".Activity.RegisterActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="32dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2">
<EditText
android:id="@+id/username_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号"
android:textColor="#4d7ea8"
android:textColorHint="#4d7ea8" />
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="请输入密码"
android:inputType="textPassword"
android:textColor="#85a7dc"
android:textColorHint="#4d7ea8" />
<EditText
android:id="@+id/repassword_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="请再次输入密码"
android:inputType="textPassword"
android:textColor="#4d7ea8"
android:textColorHint="#4d7ea8" />
<TextView
android:id="@+id/tv_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center|right"
android:text="已有帐号,立即登录!"
android:textColor="#4d7ea8"
android:textSize="14sp"
android:textStyle="bold" />
<Button
android:id="@+id/register_button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="50dp"
android:background="@drawable/lr_btn"
android:text="立 即 注 册"
android:textColor="#c3def0"
android:textSize="18sp" />
</LinearLayout>
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码
编写 RegisterActivity.java 的代码为:
package com.example.tallybook.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.tallybook.Data.DatabaseHelper;
import com.example.tallybook.R;
public class RegisterActivity extends AppCompatActivity {
private EditText mUserNameEditText;
private EditText mPasswordEditText,mRePasswordEditText;
private TextView tvLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mUserNameEditText = findViewById(R.id.username_edittext);
mPasswordEditText = findViewById(R.id.password_edittext);
mRePasswordEditText = findViewById(R.id.repassword_edittext);
tvLogin = findViewById(R.id.tv_login);
// 跳转登陆页面
tvLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
});
Button registerButton = findViewById(R.id.register_button);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取输入的账号密码
String username = mUserNameEditText.getText().toString().trim();
String password = mPasswordEditText.getText().toString().trim();
String repassword = mRePasswordEditText.getText().toString().trim();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(getApplicationContext(), "请输入账号或密码", Toast.LENGTH_SHORT).show();
return;
}
if (!password.equals(repassword)) {
Toast.makeText(getApplicationContext(), "密码不一致,请重新输入", Toast.LENGTH_SHORT).show();
return;
}
boolean result = mDatabaseHelper.insertData(username, password);
if (result) {
Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_SHORT).show();
}
}
});
}
}
复制代码
步骤 6:实现用户登录
在 activity包上右键创建 New—>Activity—>Empty Activity 选项的对话框下输入 LoginActivity 创建,同时会在 res-layout 生成 activity_login.xml 文件.
activity_login.xml页面代码如下所示L:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c3def0"
tools:context=".Activity.LoginActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="32dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView">
<EditText
android:id="@+id/username_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号"
android:textColor="#4d7ea8"
android:textColorHint="#4d7ea8" />
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="请输入密码"
android:inputType="textPassword"
android:textColor="#4d7ea8"
android:textColorHint="#4d7ea8" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|right"
android:text="忘记密码?"
android:textColor="#4d7ea8"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|right"
android:text="还没有账号?立即注册!"
android:textColor="#4d7ea8"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="60dp"
android:background="@drawable/lr_btn"
android:text="立 即 登 录"
android:textColor="#c3def0"
android:textSize="18sp" />
</LinearLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码
对应的Java页面代码如下所示:
package com.example.tallybook.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.tallybook.Data.DatabaseHelper;
import com.example.tallybook.R;
public class LoginActivity extends AppCompatActivity {
private EditText mUserNameEditText;
private EditText mPasswordEditText;
private Button mLoginButton;
private TextView rEgisterButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mUserNameEditText = findViewById(R.id.username_edittext);
mPasswordEditText = findViewById(R.id.password_edittext);
mLoginButton = findViewById(R.id.login_button);
rEgisterButton = findViewById(R.id.register_button);
// 跳转注册页面
rEgisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
finish();
}
});
// 登陆验证并跳转主页面
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取输入的账号密码
String username = mUserNameEditText.getText().toString().trim();
String password = mPasswordEditText.getText().toString().trim();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(getApplicationContext(), "请输入账号或密码", Toast.LENGTH_SHORT).show();
return;
}
boolean result = mDatabaseHelper.checkUser(username, password);
if (result) {
Toast.makeText(getApplicationContext(), "登陆成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(getApplicationContext(), "账号或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
}
}
复制代码
编译代码启动 App,在登录页面(LoginActivity)的注册按钮中点击跳转注册 页面(RegisterActivity)进行注册,注册成功后主动返回登录页面进行登录 操作,登录成功之后正常跳转主页面(MainActivity)。
步骤 7:实现主页面编写
我们先来看activity_main.xml代码,详情结构代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c3def0"
tools:context=".Activity.MainActivity">
<FrameLayout
android:id="@+id/fr"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.533"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#AAD3EF"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_home"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_weight="1"
android:src="@drawable/home" />
<ImageView
android:id="@+id/img_records"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_weight="1"
android:src="@drawable/records" />
<ImageView
android:id="@+id/img_bill"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_weight="1"
android:src="@drawable/bill" />
<ImageView
android:id="@+id/img_mine"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_weight="1"
android:src="@drawable/mine" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_home"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="首页"
android:textColor="#e6e6e6"
android:textSize="8sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_records"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="记一笔"
android:textColor="#e6e6e6"
android:textSize="8sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_bill"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="历史账单"
android:textColor="#e6e6e6"
android:textSize="8sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_mine"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="我的"
android:textColor="#e6e6e6"
android:textSize="8sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码
接下来我们看逻辑代码,这里我们实现的是页面跳转功能,详情代码如下所示:
package com.example.tallybook.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.tallybook.R;
public class MainActivity extends AppCompatActivity {
private ImageView imgHome;
private ImageView imgRecords;
private ImageView imgBill;
private ImageView imgMine;
private TextView tvHome;
private TextView tvRecords;
private TextView tvBill;
private TextView tvMine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
Navigation();
imgHome.callOnClick();//进入页面默认点击首页
}
// 点击控件进行页面转换
private void Navigation() {
// 首页
imgHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
// 记账页面
imgRecords.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
// 历史账单页面
imgBill.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
// 我的页面
imgMine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
private void initView() {
imgHome = findViewById(R.id.img_home);
imgRecords = findViewById(R.id.img_records);
imgBill = findViewById(R.id.img_bill);
imgMine = findViewById(R.id.img_mine);
tvHome = findViewById(R.id.tv_home);
tvRecords = findViewById(R.id.tv_records);
tvBill = findViewById(R.id.tv_bill);
tvMine = findViewById(R.id.tv_mine);
}
}
复制代码
步骤 8:记账页面编写
我们先来看结构代码,这里我们用到了ViewPAge列表来显示,详情代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c3def0"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<com.google.android.material.tabs.TabLayout
android:id="@+id/record_tabs"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
app:tabGravity="center"
app:tabIndicatorColor="@color/black"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/black"
app:tabTextColor="@color/grey_7D7D7D" />
</RelativeLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/record_vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c3def0" />
</LinearLayout>
复制代码
然后我们进行逻辑代码的编写,实现显示添加付出收入记录功能
package com.example.tallybook.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.tallybook.Adapter.RecordPagerAdapter;
import com.google.android.material.tabs.TabLayout;
import com.example.tallybook.R;
import java.util.ArrayList;
import java.util.List;
public class RecordsFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_records, container, false);
//1.查找控件
tabLayout = view.findViewById(R.id.record_tabs);
viewPager = view.findViewById(R.id.record_vp);
//2.设置ViewPager加载页面
initPager();
return view;
}
private void initPager() {
// 创建适配器
RecordPagerAdapter pagerAdapter = new RecordPagerAdapter(getChildFragmentManager(), fragmentList);
// 设置适配器
viewPager.setAdapter(pagerAdapter);
//将TabLayout和ViwePager进行关联
tabLayout.setupWithViewPager(viewPager);
}
}
复制代码
步骤 9:历史记录页面编写
这里直使用listview列表显示内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c3def0"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/history_record"
android:textColor="#4d7ea8"
android:textSize="24sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/history_iv_rili"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:onClick="onClick"
android:src="@mipmap/it_rili" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/history_tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2024年01月"
android:textColor="#4d7ea8"
android:textStyle="bold" />
</RelativeLayout>
<ListView
android:id="@+id/history_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="5dp"
android:padding="10dp" />
</LinearLayout>
复制代码
然后对应的逻辑代码如下所示:
package com.example.tallybook.Fragment;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.tallybook.R;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.tallybook.Adapter.AccountAdapter;
import com.example.tallybook.Bean.AccountBean;
import com.example.tallybook.Data.DBManager;
import com.example.tallybook.utils.CalendarDialog;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class BillFragment extends Fragment {
ListView historyLv;
TextView timeTv;
private ImageView riliIv;
List<AccountBean> mDatas;
AccountAdapter adapter;
int year, month;
int dialogSelPos = -1;
int dialogSelMonth = -1;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bill, container, false);
historyLv = view.findViewById(R.id.history_lv);
timeTv = view.findViewById(R.id.history_tv_time);
riliIv = view.findViewById(R.id.history_iv_rili);
mDatas = new ArrayList<>();
// 设置适配器
adapter = new AccountAdapter(getActivity(), mDatas);
historyLv.setAdapter(adapter);
initTime();
timeTv.setText(year + "年" + month + "月");
loadData(year, month);
setLVClickListener();
return view;
}
/*设置ListView每一个item的长按事件*/
private void setLVClickListener() {
historyLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
AccountBean accountBean = mDatas.get(position);
deleteItem(accountBean);
return false;
}
});
}
/* 获取指定年份月份收支情况的列表*/
private void loadData(int year, int month) {
List<AccountBean> list = DBManager.getAccountListOneMonthFromAccounttb(year, month);
mDatas.clear();
mDatas.addAll(list);
adapter.notifyDataSetChanged();
}
private void initTime() {
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
}
}
复制代码
步骤10:我的页面的编写
页面结构代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c3def0"
tools:context=".Fragment.MineFragment">
<ImageView
android:id="@+id/img_logo"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginTop="16dp"
android:src="@drawable/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view4"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:background="@drawable/fragment_home_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/img_logo" />
<LinearLayout
android:id="@+id/ll_clear"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/view4"
app:layout_constraintEnd_toEndOf="@+id/view4"
app:layout_constraintHorizontal_bias="0.39"
app:layout_constraintStart_toStartOf="@+id/view4"
app:layout_constraintTop_toTopOf="@+id/view4">
<ImageView
android:id="@+id/imageView3"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/clear" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="清空记录"
android:textColor="#183878"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="10dp"
android:layout_height="10dp"
android:src="@drawable/jiantou" />
</LinearLayout>
<View
android:id="@+id/view11"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:background="@drawable/fragment_home_view"
app:layout_constraintEnd_toEndOf="@+id/view4"
app:layout_constraintStart_toStartOf="@+id/view4"
app:layout_constraintTop_toBottomOf="@+id/view4" />
<LinearLayout
android:id="@+id/ll_ss"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="4dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/view11"
app:layout_constraintEnd_toEndOf="@+id/view11"
app:layout_constraintStart_toStartOf="@+id/view11"
app:layout_constraintTop_toTopOf="@+id/view11">
<ImageView
android:id="@+id/imageView8"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/ss" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="搜索账单"
android:textColor="#183878"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView9"
android:layout_width="10dp"
android:layout_height="10dp"
android:src="@drawable/jiantou" />
</LinearLayout>
<View
android:id="@+id/view"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:background="@drawable/fragment_home_view"
app:layout_constraintEnd_toEndOf="@+id/view11"
app:layout_constraintStart_toStartOf="@+id/view11"
app:layout_constraintTop_toBottomOf="@+id/view11" />
<LinearLayout
android:id="@+id/ll_statistics"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="4dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/view"
app:layout_constraintEnd_toEndOf="@+id/view"
app:layout_constraintStart_toStartOf="@+id/view"
app:layout_constraintTop_toTopOf="@+id/view">
<ImageView
android:id="@+id/imageView10"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/tj" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="账单统计"
android:textColor="#183878"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="10dp"
android:layout_height="10dp"
android:src="@drawable/jiantou" />
</LinearLayout>
<View
android:id="@+id/view2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:background="@drawable/fragment_home_view"
app:layout_constraintEnd_toEndOf="@+id/view"
app:layout_constraintStart_toStartOf="@+id/view"
app:layout_constraintTop_toBottomOf="@+id/view5" />
<LinearLayout
android:id="@+id/ll_changePassword"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="4dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/view2"
app:layout_constraintEnd_toEndOf="@+id/view2"
app:layout_constraintStart_toStartOf="@+id/view2"
app:layout_constraintTop_toTopOf="@+id/view2">
<ImageView
android:id="@+id/imageView11"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/modifypass" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="修改密码"
android:textColor="#183878"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="10dp"
android:layout_height="10dp"
android:src="@drawable/jiantou" />
</LinearLayout>
<View
android:id="@+id/view5"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:background="@drawable/fragment_home_view"
app:layout_constraintEnd_toEndOf="@+id/view"
app:layout_constraintStart_toStartOf="@+id/view"
app:layout_constraintTop_toBottomOf="@+id/view" />
<LinearLayout
android:id="@+id/ll_budget"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="4dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/view5"
app:layout_constraintEnd_toEndOf="@+id/view5"
app:layout_constraintStart_toStartOf="@+id/view5"
app:layout_constraintTop_toTopOf="@+id/view5">
<ImageView
android:id="@+id/imageView13"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/budget" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="设置预算"
android:textColor="#183878"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView14"
android:layout_width="10dp"
android:layout_height="10dp"
android:src="@drawable/jiantou" />
</LinearLayout>
<View
android:id="@+id/view3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:background="@drawable/fragment_home_view"
app:layout_constraintEnd_toEndOf="@+id/view2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/view2"
app:layout_constraintTop_toBottomOf="@+id/view2" />
<LinearLayout
android:id="@+id/ll_edit"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="4dp"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="@+id/view3"
app:layout_constraintEnd_toEndOf="@+id/view3"
app:layout_constraintStart_toStartOf="@+id/view3"
app:layout_constraintTop_toTopOf="@+id/view3">
<ImageView
android:id="@+id/imageView12"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/edit" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="退出软件"
android:textColor="#183878"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="10dp"
android:layout_height="10dp"
android:src="@drawable/jiantou" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码
MineFragment是一个用于显示用户个人信息和提供个人设置功能的Android Fragment类。它包含了清空账单、搜刮账单、账单统计、设置预算、修改密码和退出软件等功能。
通过点击差别的按钮,用户可以执行相应的操作,比方删除所有记录、跳转到搜刮账单界面、跳转到账单统计界面、设置预算金额、修改密码和退出软件。此外,MineFragment还使用了DatabaseHelper类和SharedPreferences类来进行数据库操作和存储预算金额。
package com.example.tallybook.Fragment;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import com.example.tallybook.Activity.LoginActivity;
import com.example.tallybook.Activity.SearchActivity;
import com.example.tallybook.Activity.StatisticsActivity;
import com.example.tallybook.Data.DBManager;
import com.example.tallybook.Data.DatabaseHelper;
import com.example.tallybook.R;
import com.example.tallybook.utils.BudgetDialog;
public class MineFragment extends Fragment {
private LinearLayout llEdit, llChangePassword, llStatistics, llClear, llSs, llBudget;
private DatabaseHelper mDatabaseHelper;
SharedPreferences preferences;
int year,month,day;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mine, container, false);
initView(view);
clear();
ss();
statistics();
budget();
changePassword();
edit();
mDatabaseHelper = new DatabaseHelper(getContext());
preferences = getActivity().getSharedPreferences("budget", Context.MODE_PRIVATE);
return view;
}
// 清空账单
private void clear() {
llClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("清空提示:")
.setMessage("您确定要删除所有记录么?\n* 删除后无法恢复,请慎重选择!")
.setPositiveButton("取消", null)
.setNegativeButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DBManager.deleteAllAccount();
preferences.edit().clear().apply();
Toast.makeText(getActivity(), "删除成功!", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
}
});
}
// 搜索账单
private void ss() {
llSs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(getActivity(), SearchActivity.class); //跳转界面
startActivity(it);
}
});
}
// 账单统计
private void statistics() {
llStatistics.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), StatisticsActivity.class);
startActivity(intent);
}
});
}
private void initView(View view) {
llClear = view.findViewById(R.id.ll_clear);
llStatistics = view.findViewById(R.id.ll_statistics);
llChangePassword = view.findViewById(R.id.ll_changePassword);
llEdit = view.findViewById(R.id.ll_edit);
llSs = view.findViewById(R.id.ll_ss);
llBudget = view.findViewById(R.id.ll_budget);
}
}
复制代码
步骤11:账单搜刮页面的编写
这个页面比力简单,使用listview列表和数据库查询显示完成,我们先来看结构文件代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c3def0"
android:orientation="vertical">
<View
android:id="@+id/view10"
android:layout_width="0dp"
android:layout_height="55dp"
android:background="#4d7ea8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/search_iv_back"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="16dp"
android:onClick="onClick"
android:src="@mipmap/it_back"
app:layout_constraintBottom_toBottomOf="@+id/view10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/search_et"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
android:background="@drawable/dialog_btn_bg"
android:hint="@string/please_search_info"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view10">
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账单搜索"
android:textColor="#c3def0"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/view10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view10" />
<ImageView
android:id="@+id/search_iv_sh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:onClick="onClick"
android:src="@mipmap/search"
app:layout_constraintBottom_toBottomOf="@+id/search_et"
app:layout_constraintEnd_toEndOf="@+id/search_et"
app:layout_constraintTop_toTopOf="@+id/search_et" />
<ListView
android:id="@+id/search_lv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:divider="#c3def0"
android:dividerHeight="6dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search_et" />
<TextView
android:id="@+id/search_tv_empty"
android:layout_width="229dp"
android:layout_height="211dp"
android:drawableTop="@mipmap/it_searchtext"
android:gravity="center"
android:text="@string/date_empty"
android:textSize="18sp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search_et" />
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码
通过用户输入关键词进行搜刮,并将搜刮结果显示在ListView中。该类使用了DBManager类来进行数据库操作,并使用了AccountAdapter类来显示搜刮结果。通过点击差别的按钮,用户可以执行返回和搜刮操作。
package com.example.tallybook.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.tallybook.Adapter.AccountAdapter;
import com.example.tallybook.Bean.AccountBean;
import com.example.tallybook.Data.DBManager;
import com.example.tallybook.R;
import java.util.ArrayList;
import java.util.List;
public class SearchActivity extends AppCompatActivity {
ListView searchLv;
EditText searchEt;
TextView emptyTv;
List<AccountBean> mDatas; //数据源
AccountAdapter adapter; //适配器对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
initView();
searchLv.setEmptyView(emptyTv); //设置无数局时,显示的控件
}
private void initView() {
searchEt = findViewById(R.id.search_et);
searchLv = findViewById(R.id.search_lv);
emptyTv = findViewById(R.id.search_tv_empty);
}
}
复制代码
步骤12:统计页面的编写
这里我们还是先进行页面结构对的编写
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c3def0"
android:orientation="vertical"
tools:context=".Activity.StatisticsActivity">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:gravity="center"
android:orientation="horizontal"
android:visibility="visible"
app:layout_constraintBottom_toTopOf="@+id/chart_vp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/chart_tv_date"
app:layout_constraintTop_toBottomOf="@+id/view12">
<Button
android:id="@+id/chart_btn_out"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginRight="10dp"
android:background="@drawable/main_recordbtn_bg"
android:text="@string/out"
android:textColor="@color/white"
android:textStyle="bold" />
<Button
android:id="@+id/chart_btn_in"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginLeft="10dp"
android:background="@drawable/dialog_btn_bg"
android:text="@string/in"
android:textColor="#4d7ea8"
android:textStyle="bold" />
</LinearLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/chart_vp"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chart_tv_in" />
<View
android:id="@+id/view12"
android:layout_width="0dp"
android:layout_height="55dp"
android:background="#4d7ea8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/chart_iv_back"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="16dp"
android:src="@mipmap/it_back"
app:layout_constraintBottom_toBottomOf="@+id/view12"
app:layout_constraintStart_toStartOf="@+id/view12"
app:layout_constraintTop_toTopOf="@+id/view12" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chart_info"
android:textColor="#c3def0"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/view12"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/chart_iv_rili"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginEnd="16dp"
android:src="@mipmap/it_rili"
app:layout_constraintBottom_toBottomOf="@+id/view12"
app:layout_constraintEnd_toEndOf="@+id/view12"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/chart_tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="2024年01月账单统计"
android:textColor="#4d7ea8"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view12" />
<TextView
android:id="@+id/chart_tv_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="共支出0笔,合计"
android:textColor="#4d7ea8"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/chart_tv_date"
app:layout_constraintTop_toBottomOf="@+id/chart_tv_date" />
<TextView
android:id="@+id/chart_tv_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="8dp"
android:gravity="top|left"
android:text="0.0"
android:textColor="#ffffff"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/textView14"
app:layout_constraintTop_toBottomOf="@+id/chart_tv_out" />
<TextView
android:id="@+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:text="¥"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/chart_tv_out"
app:layout_constraintTop_toBottomOf="@+id/chart_tv_out" />
</androidx.constraintlayout.widget.ConstraintLayout>
复制代码
该活动包含了收入和付出的按钮、日期显示、统计数据显示以及ViewPager等控件。通过点击差别的按钮,用户可以切换显示收入或付出的统计数据。该活动还包含了初始化控件、初始化时间、初始化Fragment、统计数据的初始化和显示、返回按钮的点击变乱、显示日历对话框等方法。通过使用DBManager类进行数据库操作,获取收入和付出的统计数据,并使用ChartVPAdapter适配器类将Fragment添加到ViewPager中进行显示。用户可以通过点击日历图标选择差别的日期进行统计。
package com.example.tallybook.Activity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.tallybook.Adapter.ChartVPAdapter;
import com.example.tallybook.Data.DBManager;
import com.example.tallybook.Fragment.IncomChartFragment;
import com.example.tallybook.Fragment.OutcomChartFragment;
import com.example.tallybook.R;
import com.example.tallybook.utils.CalendarDialog;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class StatisticsActivity extends AppCompatActivity {
Button inBtn, outBtn;
TextView dateTv, inTv, outTv;
ViewPager chartVp;
int year;
int month;
int selectPos = -1, selectMonth = -1;
List<Fragment> chartFragList;
private IncomChartFragment incomChartFragment;
private OutcomChartFragment outcomChartFragment;
private ChartVPAdapter chartVPAdapter;
private ImageView chartIV, chartBack;
private float inMoneyOneMonth, outMoneyOneMonth;
private int incountItemOneMonth, outcountItemOneMonth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statistics);
initView();
initTime();
}
// 显示日历
private void rili() {
chartIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCalendarDialog();
}
});
}
// 返回
private void back() {
chartBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void setVPSelectListener() {
chartVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
setButtonStyle(position);
}
});
}
/* 统计某年某月的收支情况数据*/
private void initStatistics(int year, int month) {
inMoneyOneMonth = DBManager.getSumMoneyOneMonth(year, month, 1); //收入总钱数
outMoneyOneMonth = DBManager.getSumMoneyOneMonth(year, month, 0); //支出总钱数
incountItemOneMonth = DBManager.getCountItemOneMonth(year, month, 1); //收入多少笔
outcountItemOneMonth = DBManager.getCountItemOneMonth(year, month, 0); //支出多少笔
dateTv.setText(year + "年" + month + "月账单统计");
// 收入
inBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setButtonStyle(1);
chartVp.setCurrentItem(1);
outTv.setText("共收入" + incountItemOneMonth + "笔,合计");
inTv.setText(String.valueOf(inMoneyOneMonth));
}
});
// 支出
outBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setButtonStyle(0);
chartVp.setCurrentItem(0);
outTv.setText("共支出" + outcountItemOneMonth + "笔支出,合计");
inTv.setText(String.valueOf(outMoneyOneMonth));
}
});
}
/**
* 初始化时间的方法
*/
private void initTime() {
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
}
/**
* 初始化控件
*/
private void initView() {
inBtn = findViewById(R.id.chart_btn_in);
outBtn = findViewById(R.id.chart_btn_out);
dateTv = findViewById(R.id.chart_tv_date);
inTv = findViewById(R.id.chart_tv_in);
outTv = findViewById(R.id.chart_tv_out);
chartVp = findViewById(R.id.chart_vp);
chartIV = findViewById(R.id.chart_iv_rili);
chartBack = findViewById(R.id.chart_iv_back);
}
/* 设置按钮样式的改变 支出-0 收入-1*/
private void setButtonStyle(int kind) {
if (kind == 0) {
outBtn.setBackgroundResource(R.drawable.main_recordbtn_bg);
outBtn.setTextColor(Color.WHITE);
inBtn.setBackgroundResource(R.drawable.dialog_btn_bg);
inBtn.setTextColor(Color.BLACK);
} else if (kind == 1) {
inBtn.setBackgroundResource(R.drawable.main_recordbtn_bg);
inBtn.setTextColor(Color.WHITE);
outBtn.setBackgroundResource(R.drawable.dialog_btn_bg);
outBtn.setTextColor(Color.BLACK);
}
}
}
复制代码
至此,完备的小熊记账本项目就创建完成了。
四、获取源码
关注公众号
《编程乐学》
,后台复兴:
24010601
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4