使用android studio编写个人理财要求提供登录界面给用户 3. 要求有界面让用户输入每天的收入和支出(包括但不限于以下字段:项目名称,金额,录入时间,类别(信用卡/现金) 4. 可以对某笔记录添加文字或者照片备注。 5. 支持按照每日,每月,每年进行统计、查询。 6. 以上内容要求记入SQLITE或者文件系统中,APP重启后不丢失。的代码
时间: 2023-06-19 19:03:30 浏览: 162
很抱歉,由于篇幅限制,无法提供完整的代码。但我可以提供一些思路和参考代码供您参考。
首先,您需要创建一个登录界面,可以使用 Android Studio 提供的模板来快速创建。登录界面需要提供用户名和密码输入框,以及登录按钮。在用户输入正确的用户名和密码后,可以跳转到主界面。
在主界面中,您需要创建一个收支记录列表,用于显示用户输入的收入和支出记录。您可以使用 RecyclerView 来实现列表,每个列表项需要包含项目名称、金额、录入时间和类别等信息。同时,您需要提供一个添加记录的按钮,点击后跳转到添加记录界面。
在添加记录界面中,您需要提供输入框让用户输入每天的收入和支出,以及选择类别(信用卡或现金),并且可以添加文字备注或者照片备注。添加记录界面需要提供一个保存按钮,点击后将记录保存到 SQLite 数据库或者文件系统中。
最后,您需要提供一个统计查询界面,用户可以选择按照每日、每月或每年进行统计查询。您可以使用 Android Studio 提供的图表库来实现图表展示。
以下是参考代码:
1. 创建登录界面
activity_login.xml
```xml
<LinearLayout
...
android:orientation="vertical">
<EditText
...
android:id="@+id/etUsername"
android:hint="用户名"/>
<EditText
...
android:id="@+id/etPassword"
android:hint="密码"
android:inputType="textPassword"/>
<Button
...
android:text="登录"
android:onClick="onLoginButtonClick"/>
</LinearLayout>
```
LoginActivity.java
```java
public class LoginActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUsername = findViewById(R.id.etUsername);
etPassword = findViewById(R.id.etPassword);
}
public void onLoginButtonClick(View view) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
// TODO: 验证用户名和密码是否正确
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
```
2. 创建主界面和收支记录列表
activity_main.xml
```xml
<LinearLayout
...
android:orientation="vertical">
<Button
...
android:text="添加记录"
android:onClick="onAddRecordButtonClick"/>
<androidx.recyclerview.widget.RecyclerView
...
android:id="@+id/rvRecords"/>
</LinearLayout>
```
RecordAdapter.java
```java
public class RecordAdapter extends RecyclerView.Adapter<RecordAdapter.RecordViewHolder> {
private List<Record> recordList;
public RecordAdapter(List<Record> recordList) {
this.recordList = recordList;
}
@NonNull
@Override
public RecordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_record, parent, false);
return new RecordViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecordViewHolder holder, int position) {
Record record = recordList.get(position);
holder.tvName.setText(record.getName());
holder.tvAmount.setText(String.format(Locale.getDefault(), "%.2f", record.getAmount()));
holder.tvTime.setText(record.getTime());
holder.tvType.setText(record.getType());
}
@Override
public int getItemCount() {
return recordList.size();
}
public static class RecordViewHolder extends RecyclerView.ViewHolder {
public TextView tvName;
public TextView tvAmount;
public TextView tvTime;
public TextView tvType;
public RecordViewHolder(@NonNull View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tvName);
tvAmount = itemView.findViewById(R.id.tvAmount);
tvTime = itemView.findViewById(R.id.tvTime);
tvType = itemView.findViewById(R.id.tvType);
}
}
}
```
MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
private List<Record> recordList;
private RecordAdapter recordAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recordList = new ArrayList<>();
recordAdapter = new RecordAdapter(recordList);
RecyclerView rvRecords = findViewById(R.id.rvRecords);
rvRecords.setLayoutManager(new LinearLayoutManager(this));
rvRecords.setAdapter(recordAdapter);
}
public void onAddRecordButtonClick(View view) {
Intent intent = new Intent(this, AddRecordActivity.class);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
Record record = data.getParcelableExtra("record");
recordList.add(record);
recordAdapter.notifyDataSetChanged();
}
}
}
```
3. 创建添加记录界面
activity_add_record.xml
```xml
<LinearLayout
...
android:orientation="vertical">
<EditText
...
android:id="@+id/etName"
android:hint="项目名称"/>
<EditText
...
android:id="@+id/etAmount"
android:hint="金额"
android:inputType="numberDecimal"/>
<EditText
...
android:id="@+id/etTime"
android:hint="录入时间"/>
<RadioGroup
...
android:id="@+id/rgType">
<RadioButton
...
android:text="信用卡"/>
<RadioButton
...
android:text="现金"/>
</RadioGroup>
<Button
...
android:text="添加备注"
android:onClick="onAddRemarkButtonClick"/>
<Button
...
android:text="保存"
android:onClick="onSaveButtonClick"/>
</LinearLayout>
```
AddRecordActivity.java
```java
public class AddRecordActivity extends AppCompatActivity {
private EditText etName;
private EditText etAmount;
private EditText etTime;
private RadioGroup rgType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_record);
etName = findViewById(R.id.etName);
etAmount = findViewById(R.id.etAmount);
etTime = findViewById(R.id.etTime);
rgType = findViewById(R.id.rgType);
}
public void onAddRemarkButtonClick(View view) {
// TODO: 添加文字或者照片备注
}
public void onSaveButtonClick(View view) {
String name = etName.getText().toString();
double amount = Double.parseDouble(etAmount.getText().toString());
String time = etTime.getText().toString();
String type = rgType.getCheckedRadioButtonId() == R.id.rbCreditCard ? "信用卡" : "现金";
Record record = new Record(name, amount, time, type);
Intent intent = new Intent();
intent.putExtra("record", record);
setResult(RESULT_OK, intent);
finish();
}
}
```
4. 创建统计查询界面
activity_statistics.xml
```xml
<LinearLayout
...
android:orientation="vertical">
<RadioGroup
...
android:id="@+id/rgTimeSpan">
<RadioButton
...
android:text="每日"/>
<RadioButton
...
android:text="每月"/>
<RadioButton
...
android:text="每年"/>
</RadioGroup>
<Button
...
android:text="查询"
android:onClick="onQueryButtonClick"/>
<com.github.mikephil.charting.charts.BarChart
...
android:id="@+id/chart"/>
</LinearLayout>
```
StatisticsActivity.java
```java
public class StatisticsActivity extends AppCompatActivity {
private BarChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statistics);
chart = findViewById(R.id.chart);
}
public void onQueryButtonClick(View view) {
// TODO: 根据时间段统计查询
List<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(1, 100));
entries.add(new BarEntry(2, 200));
entries.add(new BarEntry(3, 300));
BarDataSet dataSet = new BarDataSet(entries, "统计结果");
dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
BarData barData = new BarData(dataSet);
chart.setData(barData);
chart.invalidate();
}
}
```
5. 创建 Record 实体类
```java
public class Record implements Parcelable {
private String name;
private double amount;
private String time;
private String type;
public Record(String name, double amount, String time, String type) {
this.name = name;
this.amount = amount;
this.time = time;
this.type = type;
}
protected Record(Parcel in) {
name = in.readString();
amount = in.readDouble();
time = in.readString();
type = in.readString();
}
public static final Creator<Record> CREATOR = new Creator<Record>() {
@Override
public Record createFromParcel(Parcel in) {
return new Record(in);
}
@Override
public Record[] newArray(int size) {
return new Record[size];
}
};
public String getName() {
return name;
}
public double getAmount() {
return amount;
}
public String getTime() {
return time;
}
public String getType() {
return type;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeDouble(amount);
dest.writeString(time);
dest.writeString(type);
}
}
```
6. 创建 SQLite 数据库
RecordDatabaseHelper.java
```java
public class RecordDatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "records.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "record";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_AMOUNT = "amount";
public static final String COLUMN_TIME = "time";
public static final String COLUMN_TYPE = "type";
public RecordDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTableSql = "CREATE TABLE " + TABLE_NAME + " (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT NOT NULL, " +
COLUMN_AMOUNT + " REAL NOT NULL, " +
COLUMN_TIME + " TEXT NOT NULL, " +
COLUMN_TYPE + " TEXT NOT NULL)";
db.execSQL(createTableSql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO: 数据库升级操作
}
public long insertRecord(Record record) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, record.getName());
values.put(COLUMN_AMOUNT, record.getAmount());
values.put(COLUMN_TIME, record.getTime());
values.put(COLUMN_TYPE, record.getType());
long id = db.insert(TABLE_NAME, null, values);
db.close();
return id;
}
public List<Record> queryRecords() {
SQLiteDatabase db = getReadableDatabase();
String[] projection = {
COLUMN_NAME,
COLUMN_AMOUNT,
COLUMN_TIME,
COLUMN_TYPE
};
Cursor cursor = db.query(
TABLE_NAME,
projection,
null,
null,
null,
null,
null
);
List<Record> recordList = new ArrayList<>();
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NAME));
double amount = cursor.getDouble(cursor.getColumnIndexOrThrow(COLUMN_AMOUNT));
String time = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_TIME));
String type = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_TYPE));
Record record = new Record(name, amount, time, type);
recordList.add(record);
}
cursor.close();
db.close();
return recordList;
}
}
```
AddRecordActivity.java 中调用数据库操作:
```java
public void onSaveButtonClick(View view) {
...
Record record = new Record(name, amount, time, type);
RecordDatabaseHelper dbHelper = new RecordDatabaseHelper(this);
dbHelper.insertRecord(record);
dbHelper.close();
...
}
```
MainActivity.java 中调用数据库操作:
```java
public class MainActivity extends AppCompatActivity {
private List<Record> recordList;
private RecordAdapter recordAdapter;
private RecordDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new RecordDatabaseHelper(this);
recordList = dbHelper.queryRecords();
recordAdapter = new RecordAdapter(recordList);
RecyclerView rvRecords = findViewById(R.id.rvRecords);
rvRecords.setLayoutManager(new LinearLayoutManager(this));
rvRecords.setAdapter(recordAdapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
Record record = data.getParcelableExtra("record");
long id = dbHelper.insertRecord(record);
record.setId(id);
recordList.add(record);
recordAdapter.notifyDataSetChanged();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
dbHelper.close();
}
}
```
阅读全文