记账本实现添加功能
我们在MainActivity.java里面写了addAccount,即添加功能,点击加号跳转到一个新的页面,之后完成想要执行的操作,然后具体执行的操作我们写在new_cost.java里面
package com.example.cashBook; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.DatePicker; import android.widget.EditText; import android.widget.Toast; public class new_cost extends AppCompatActivity { private DBHelper helper; private EditText et_cost_title; private EditText et_cost_money; private DatePicker dp_cost_date; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_cost); initView(); } private void initView() { helper = new DBHelper(new_cost.this); et_cost_title = findViewById(R.id.et_cost_title); et_cost_money = findViewById(R.id.et_cost_money); dp_cost_date = findViewById(R.id.dp_cost_date); } public void okButton(View view) { String titleStr = et_cost_title.getText().toString().trim(); String moneyStr = et_cost_money.getText().toString().trim(); String dateStr = dp_cost_date.getYear() + "-" + (dp_cost_date.getMonth() + 1) + "-" + dp_cost_date.getDayOfMonth();//这里getMonth会比当前月份少一个月,所以要+1 if ("".equals(moneyStr)) {//可以不填写Title但是不能不填金额 Toast toast = Toast.makeText(this, "请填写金额", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } else { SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("Title", titleStr); values.put("Money", moneyStr); values.put("Date", dateStr); long account = db.insert("account", null, values); if (account > 0) { Toast toast = Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); setResult(1); finish(); } else { Toast toast = Toast.makeText(this, "请重试", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); db.close(); } setResult(1); finish(); } } }
将new_cost写好之后,然后会在layout里面写一个activity_new_cost.xml文件,就是页面布局文件(同时生成new_cost文件和activity_new_cost文件)
<?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:orientation="vertical" android:gravity="center"> <EditText android:id="@+id/et_cost_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="4dp" android:hint="Cost Title" android:textColor="#ffbd27" /> <EditText android:id="@+id/et_cost_money" android:inputType="number|numberDecimal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="4dp" android:hint="Cost Money" android:textColor="#ffbd27" /> <DatePicker android:id="@+id/dp_cost_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:datePickerMode="spinner" android:calendarViewShown="false" /> <Button android:onClick="okButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确认" android:textSize="20dp" android:textColor="#333333" android:background="#ffbd27" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" /> LinearLayout>