写一个安卓记账本的程序
时间: 2023-10-01 08:11:33 浏览: 85
基于Android Studio开发的记账本APP源码,有登录注册界面,可以实现消费的记录的增加与删除,Android毕业设计
5星 · 资源好评率100%
以下是一个简单的安卓记账本程序的示例:
1. 创建数据库:
在应用程序中创建SQLite数据库并创建相应的表格。
```
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "account.db";
private static final int DB_VERSION = 1;
// 用户表
private static final String TABLE_USER = "user";
private static final String USER_ID = "id";
private static final String USER_NAME = "name";
private static final String USER_PASSWORD = "password";
// 账单表
private static final String TABLE_BILL = "bill";
private static final String BILL_ID = "id";
private static final String BILL_TYPE = "type";
private static final String BILL_AMOUNT = "amount";
private static final String BILL_DATE = "date";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 创建用户表
db.execSQL("CREATE TABLE " + TABLE_USER + " ("
+ USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ USER_NAME + " TEXT,"
+ USER_PASSWORD + " TEXT)");
// 创建账单表
db.execSQL("CREATE TABLE " + TABLE_BILL + " ("
+ BILL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ BILL_TYPE + " INTEGER,"
+ BILL_AMOUNT + " REAL,"
+ BILL_DATE + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 数据库升级操作
}
}
```
2. 创建登录/注册界面:
在安卓应用程序中,登录和注册界面是非常常见的。通常情况下,注册界面包括用户名、密码和确认密码等输入框,用户需要输入相应的信息进行注册。登录界面包括用户名和密码的输入框,用户需要输入正确的用户名和密码才能登录。
```
public class LoginActivity extends AppCompatActivity {
private EditText mEtUserName;
private EditText mEtPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mEtUserName = findViewById(R.id.et_user_name);
mEtPassword = findViewById(R.id.et_password);
Button btnLogin = findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName = mEtUserName.getText().toString();
String password = mEtPassword.getText().toString();
// 验证用户名和密码是否正确
if (checkUser(userName, password)) {
// 登录成功,跳转到主界面
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
// 登录失败,弹出提示框
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
TextView tvRegister = findViewById(R.id.tv_register);
tvRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到注册界面
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
private boolean checkUser(String userName, String password) {
// 验证用户名和密码是否正确
return false;
}
}
public class RegisterActivity extends AppCompatActivity {
private EditText mEtUserName;
private EditText mEtPassword;
private EditText mEtConfirmPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mEtUserName = findViewById(R.id.et_user_name);
mEtPassword = findViewById(R.id.et_password);
mEtConfirmPassword = findViewById(R.id.et_confirm_password);
Button btnRegister = findViewById(R.id.btn_register);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName = mEtUserName.getText().toString();
String password = mEtPassword.getText().toString();
String confirmPassword = mEtConfirmPassword.getText().toString();
// 验证输入的信息是否正确
if (!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(password)
&& password.equals(confirmPassword)) {
// 注册成功,将用户信息保存到数据库中
saveUser(userName, password);
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
finish();
} else {
// 注册失败,弹出提示框
Toast.makeText(RegisterActivity.this, "注册失败,请检查输入的信息", Toast.LENGTH_SHORT).show();
}
}
});
}
private void saveUser(String userName, String password) {
// 将用户信息保存到数据库中
}
}
```
3. 创建主界面:
安卓记账本的主界面包括账单列表、记账按钮、分类统计、预算管理等功能。用户可以在此界面中进行查看账单、新增账单、分类统计等操作。
```
public class MainActivity extends AppCompatActivity {
private List<Bill> mBillList = new ArrayList<>();
private BillAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
mAdapter = new BillAdapter(mBillList);
recyclerView.setAdapter(mAdapter);
Button btnAddBill = findViewById(R.id.btn_add_bill);
btnAddBill.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到新增账单界面
Intent intent = new Intent(MainActivity.this, AddBillActivity.class);
startActivityForResult(intent, 1);
}
});
Button btnCategory = findViewById(R.id.btn_category);
btnCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到分类统计界面
Intent intent = new Intent(MainActivity.this, CategoryActivity.class);
startActivity(intent);
}
});
Button btnBudget = findViewById(R.id.btn_budget);
btnBudget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到预算管理界面
Intent intent = new Intent(MainActivity.this, BudgetActivity.class);
startActivity(intent);
}
});
// 加载账单数据
loadBills();
}
private void loadBills() {
mBillList.clear();
// 从数据库中加载账单数据
// ...
// 加载完成后更新列表
mAdapter.notifyDataSetChanged();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
// 刷新账单列表
loadBills();
}
}
}
class BillAdapter extends RecyclerView.Adapter<BillAdapter.ViewHolder> {
private List<Bill> mBillList;
static class ViewHolder extends RecyclerView.ViewHolder {
TextView tvType;
TextView tvAmount;
TextView tvDate;
public ViewHolder(View view) {
super(view);
tvType = view.findViewById(R.id.tv_type);
tvAmount = view.findViewById(R.id.tv_amount);
tvDate = view.findViewById(R.id.tv_date);
}
}
public BillAdapter(List<Bill> billList) {
mBillList = billList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.bill_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Bill bill = mBillList.get(position);
holder.tvType.setText(bill.getType());
holder.tvAmount.setText(String.valueOf(bill.getAmount()));
holder.tvDate.setText(bill.getDate());
}
@Override
public int getItemCount() {
return mBillList.size();
}
}
```
4. 创建新增账单界面:
在新增账单界面中,用户需要选择账单类型、输入账单金额、选择日期等信息。
```
public class AddBillActivity extends AppCompatActivity {
private Spinner mSpinnerType;
private EditText mEtAmount;
private TextView mTvDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_bill);
mSpinnerType = findViewById(R.id.spinner_type);
mEtAmount = findViewById(R.id.et_amount);
mTvDate = findViewById(R.id.tv_date);
Button btnSave = findViewById(R.id.btn_save);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String type = mSpinnerType.getSelectedItem().toString();
String amount = mEtAmount.getText().toString();
String date = mTvDate.getText().toString();
// 保存账单信息到数据库中
saveBill(type, amount, date);
setResult(RESULT_OK);
finish();
}
});
mTvDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog();
}
});
}
private void showDatePickerDialog() {
Calendar calendar = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String date = year + "-" + (month + 1) + "-" + dayOfMonth;
mTvDate.setText(date);
}
},
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
private void saveBill(String type, String amount, String date) {
// 将账单信息保存到数据库中
}
}
```
5. 创建分类统计界面:
在分类统计界面中,可以使用图表库来展示用户的收入和支出的分类统计信息。这里使用MPAndroidChart库来展示饼状图和柱状图。
```
public class CategoryActivity extends AppCompatActivity {
private PieChart mPieChart;
private BarChart mBarChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
mPieChart = findViewById(R.id.pie_chart);
mBarChart = findViewById(R.id.bar_chart);
// 加载分类统计数据
loadCategoryData();
}
private void loadCategoryData() {
// 从数据库中加载分类统计数据
// ...
// 加载完成后更新图表
updatePieChart();
updateBarChart();
}
private void updatePieChart() {
mPieChart.setUsePercentValues(true);
mPieChart.getDescription().setEnabled(false);
mPieChart.setExtraOffsets(5, 10, 5, 5);
mPieChart.setDrawHoleEnabled(true);
mPieChart.setHoleColor(Color.WHITE);
mPieChart.setTransparentCircleRadius(61f);
mPieChart.setDrawCenterText(true);
ArrayList<PieEntry> entries
阅读全文