使用Android studio写一个记账本,包括xml和Java文件

时间: 2023-12-03 18:47:25 浏览: 39
好的,下面是一个简单的记账本的Android Studio项目,包含XML和Java文件。 XML文件: activity_main.xml ```xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.myapp.MainActivity"> <TextView android:id="@+id/textViewTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="My Account Book" android:textAlignment="center" android:textColor="@color/colorPrimaryDark" android:textSize="24sp" android:textStyle="bold" /> <LinearLayout android:id="@+id/linearLayoutInput" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/textViewTitle" android:layout_marginTop="16dp" android:orientation="horizontal"> <EditText android:id="@+id/editTextAmount" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Amount" android:inputType="numberDecimal" android:textAlignment="center" /> <EditText android:id="@+id/editTextDescription" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Description" android:textAlignment="center" /> <Button android:id="@+id/buttonAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add" /> </LinearLayout> <ListView android:id="@+id/listViewAccounts" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/linearLayoutInput" android:layout_marginTop="16dp" /> </RelativeLayout> ``` Java文件: MainActivity.java ```java package com.example.myapp; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import java.text.DecimalFormat; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private EditText editTextAmount; private EditText editTextDescription; private Button buttonAdd; private ListView listViewAccounts; private TextView textViewTotal; private AccountAdapter accountAdapter; private ArrayList<Account> accounts = new ArrayList<>(); private double total = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextAmount = findViewById(R.id.editTextAmount); editTextDescription = findViewById(R.id.editTextDescription); buttonAdd = findViewById(R.id.buttonAdd); listViewAccounts = findViewById(R.id.listViewAccounts); textViewTotal = findViewById(R.id.textViewTotal); accountAdapter = new AccountAdapter(this, accounts); listViewAccounts.setAdapter(accountAdapter); buttonAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addAccount(); } }); } private void addAccount() { String amountString = editTextAmount.getText().toString(); String description = editTextDescription.getText().toString(); if (amountString.isEmpty() || description.isEmpty()) { return; } double amount = Double.parseDouble(amountString); Account account = new Account(amount, description); accounts.add(account); total += amount; DecimalFormat decimalFormat = new DecimalFormat("#.##"); textViewTotal.setText("Total: $" + decimalFormat.format(total)); accountAdapter.notifyDataSetChanged(); editTextAmount.setText(""); editTextDescription.setText(""); } } ``` Account.java ```java package com.example.myapp; public class Account { private double amount; private String description; public Account(double amount, String description) { this.amount = amount; this.description = description; } public double getAmount() { return amount; } public String getDescription() { return description; } } ``` AccountAdapter.java ```java package com.example.myapp; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import java.text.DecimalFormat; import java.util.ArrayList; public class AccountAdapter extends ArrayAdapter<Account> { public AccountAdapter(Context context, ArrayList<Account> accounts) { super(context, 0, accounts); } @Override public View getView(int position, View convertView, ViewGroup parent) { Account account = getItem(position); if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_account, parent, false); } TextView textViewAmount = convertView.findViewById(R.id.textViewAmount); TextView textViewDescription = convertView.findViewById(R.id.textViewDescription); DecimalFormat decimalFormat = new DecimalFormat("#.##"); textViewAmount.setText("$" + decimalFormat.format(account.getAmount())); textViewDescription.setText(account.getDescription()); return convertView; } } ``` item_account.xml ```xml <?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="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/textViewAmount" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@android:color/holo_green_dark" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/textViewDescription" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textAlignment="center" android:textSize="18sp" /> </LinearLayout> ``` 这个记账本的界面比较简单,用户可以输入金额和描述,然后点击“Add”按钮将数据添加到列表中。每个账户包含金额和描述,并且列表会显示所有添加的账户。在列表的底部,还有一个显示总金额的TextView。

相关推荐

最新推荐

recommend-type

解决Android Studio XML编辑界面不显示下面的Text和Design选项卡

主要介绍了解决Android Studio XML编辑界面不显示下面的Text和Design选项卡,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

详解Android Studio实现用户登陆界面demo(xml实现)

主要介绍了详解Android Studio实现用户登陆界面demo,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

详解如何在Android studio中更新sdk版本和build-tools版本

一、首先看下Android开发用到的sdk目录: build-tools 保存着一些Android平台相关通用工具,比如adb、和aapt、aidl、dx等文件。  aapt即Android Asset Packaging Tool , 在SDK的build-tools目录下. 该工具可以...
recommend-type

Android Studio无法执行Java类的main方法问题及解决方法

主要介绍了Android Studio无法执行Java main方法的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Android studio创建第一个app

主要为大家详细介绍了如何使用Android studio创建你的第一个项目Hello World,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。