使用Android studio写一个记账本,包括xml和Java文件
时间: 2023-12-03 21:47:25 浏览: 105
好的,下面是一个简单的记账本的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。
阅读全文