一、成果
点击添加账目
选择账目类型
点击输入日期
点击确定,输入其它信息
点击添加
这里以为第一次没截图到上面的添加成功
查看数据库
添加成功
二、经验总结
首先,今天原本打算将添加与查询全部账目的部分,但是在开发过程中遇到了一些问题,关于变量的类型,如date,是否设为Date类型,但Date类型无法强转为String类型,最终在layout布局文件里设置了inputType=date解决了问题;还有下拉框组件的点击事件,原本使用的是setOnClickListener,实际应该要用mTypeSpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 。
再就是关于选择日期,弹出日历组件,参考了下面的博客
https://blog.csdn.net/qq_40116418/article/details/84784593
三、部分原码
AddBillActivity.java
package com.example.familybook; import android.app.Activity; import android.app.DatePickerDialog; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; import androidx.annotation.Nullable; import com.example.familybook.dao.BillDaoImpl; import com.example.familybook.dao.IBillDao; import com.example.familybook.entity.Bill; import java.sql.Date; import java.util.Calendar; public class AddBillActivity extends Activity { private int mYear; private int mMonth; private int mDay; private Spinner mTypeSpin; private EditText mMoney; private EditText mDate; private EditText mRemark; private Button mAddBillBtn; private IBillDao mIBillDao; private String mTypeText; private String mUsername; private String TAG="AddBillActivity"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_addbill); // //获取登录者的用户名 Intent intent =getIntent(); mUsername =intent.getStringExtra("username"); Log.e(TAG,"使用者:"+mUsername); //获取时间 initDate(); //初始化控件 initView(); //添加监听事件 initListener(); } private void initDate() { //获取当前时间 Calendar ca = Calendar.getInstance(); mYear = ca.get(Calendar.YEAR); mMonth = ca.get(Calendar.MONTH); mDay = ca.get(Calendar.DAY_OF_MONTH); } private void initListener() { mTypeSpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { mTypeText=parent.getItemAtPosition(position).toString(); Toast.makeText(AddBillActivity.this,mTypeText,Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); mDate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //调用时间选择器 DatePickerDialog datePickerDialog = new DatePickerDialog(AddBillActivity.this, R.style.MyDatePickerDialogTheme, onDateSetListener, mYear, mMonth, mDay); //获取时间戳 long timeStamp = System.currentTimeMillis(); //设置可以显示的最晚的时间 datePickerDialog.getDatePicker().setMaxDate(timeStamp); //弹框 datePickerDialog.show(); } }); mAddBillBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addBill(); } }); } /** * 日期选择器对话监听 */ private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; String days; if (mMonth + 1 < 10) { if (mDay < 10) { days = new StringBuffer().append(mYear).append("-").append("0"). append(mMonth + 1).append("-").append("0").append(mDay).append("").toString(); } else { days = new StringBuffer().append(mYear).append("-").append("0"). append(mMonth + 1).append("-").append(mDay).append("").toString(); } } else { if (mDay < 10) { days = new StringBuffer().append(mYear).append("-"). append(mMonth + 1).append("-").append("0").append(mDay).append("").toString(); } else { days = new StringBuffer().append(mYear).append("-"). append(mMonth + 1).append("-").append(mDay).append("").toString(); } } mDate.setText(days); } }; private void addBill() { Bill bill=null; //获取账目类型 if(mTypeText==null){ //说明用户未点击按钮,选择默认首项:饮食 mTypeText="饮食"; } //获取账目金额 String moneyText=mMoney.getText().toString().trim(); //获取日期 String date= mDate.getText().toString().trim(); //获取备注 String remarkText=mRemark.getText().toString().trim(); if(TextUtils.isEmpty(moneyText)){ //账目金额为空 Toast.makeText(this,"账目金额不可以为空",Toast.LENGTH_SHORT).show(); return; }else if(TextUtils.isEmpty((CharSequence) date)){ //账目日期为空 Toast.makeText(this,"账目日期不可以为空",Toast.LENGTH_SHORT).show(); return; }else { Log.e(TAG, "账户:" + mUsername); Log.e(TAG, "类型:" + mTypeText); Log.e(TAG, "金额:" + moneyText); Log.e(TAG, "日期:" + date); Log.e(TAG, "备注:" + remarkText); bill=new Bill(mUsername,mTypeText,moneyText,date,remarkText); int rs=(int)mIBillDao.AddBill(bill); Log.e(TAG,"rs:"+rs); if (rs > -1) { //添加成功,跳回首面 Intent intent = new Intent(); setResult(2, intent); finish(); } } } private void initView() { mTypeSpin =(Spinner) this.findViewById(R.id.type_spin); mMoney =(EditText)this.findViewById(R.id.bill_money); mDate =(EditText)this.findViewById(R.id.bill_date); mRemark=(EditText)this.findViewById(R.id.bill_remark); mAddBillBtn=(Button)this.findViewById(R.id.add_bill_btn); mIBillDao =new BillDaoImpl(AddBillActivity.this); } }
Bill.java
package com.example.familybook.entity; import java.sql.Date; /** * 账单实体类 */ public class Bill { private int _id; private String username; private String type; private String money; private String date; private String remark; public Bill() { } public Bill(int _id, String username, String type, String money, String date, String remark) { this._id = _id; this.username = username; this.type = type; this.money = money; this.date = date; this.remark = remark; } public Bill(String username, String type, String money, String date, String remark) { this.username = username; this.type = type; this.money = money; this.date = date; this.remark = remark; } public int get_id() { return _id; } public void set_id(int _id) { this._id = _id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getMoney() { return money; } public void setMoney(String money) { this.money = money; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return "Bill{" + "_id=" + _id + ", username=‘" + username + ‘\‘‘ + ", type=‘" + type + ‘\‘‘ + ", money=" + money + ", date=" + date + ", remark=‘" + remark + ‘\‘‘ + ‘}‘; } }
BillDaoImpl.java
package com.example.familybook.dao; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.familybook.database.FamilyBookDatabaseHelper; import com.example.familybook.entity.Bill; import com.example.familybook.utils.Constants; import java.util.ArrayList; import java.util.List; public class BillDaoImpl implements IBillDao { private final FamilyBookDatabaseHelper mBillDatabaseHelper; public BillDaoImpl(Context context){ mBillDatabaseHelper=new FamilyBookDatabaseHelper(context); } /** * 添加账目 * @param bill * @return */ @Override public long AddBill(Bill bill) { SQLiteDatabase db =mBillDatabaseHelper.getWritableDatabase(); long result =-1; try { ContentValues values =new ContentValues(); values.put(Constants.BILL_TABLE_FIELD_UNAME,bill.getUsername()); values.put(Constants.BILL_TABLE_FIELD_TYPE,bill.getType()); values.put(Constants.BILL_TABLE_FIELD_MONEY,bill.getMoney()); values.put(Constants.BILL_TABLE_FIELD_DATE,bill.getDate()); values.put(Constants.BILL_TABLE_FIELD_REMARK,bill.getRemark()); result=db.insert(Constants.BILL_TABLE_NAME,null,values); }catch (Exception e){ e.printStackTrace(); }finally { db.close(); } return result; } }
activity_addbill.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@mipmap/addbill" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:text="添加账目" android:textSize="50sp" android:layout_gravity="center" android:layout_height="wrap_content"/> <RelativeLayout android:layout_width="match_parent" android:layout_marginTop="40dp" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="82dp" android:text="请选择账目类型" android:textSize="20sp" /> <Spinner android:id="@+id/type_spin" android:entries="@array/bill_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scrollbarSize="25sp" android:layout_alignParentRight="true" android:layout_marginRight="60dp" /> </RelativeLayout> <EditText android:id="@+id/bill_money" android:layout_marginTop="20dp" android:layout_width="250dp" android:hint="请输入账目金额" android:inputType="number" android:layout_gravity="center" android:layout_height="wrap_content"/> <EditText android:id="@+id/bill_date" android:layout_marginTop="20dp" android:layout_width="250dp" android:hint="请输入账目日期" android:inputType="date" android:layout_gravity="center" android:layout_height="wrap_content"/> <EditText android:id="@+id/bill_remark" android:layout_width="250dp" android:layout_marginTop="20dp" android:layout_gravity="center" android:hint="在此处添加备注" android:layout_height="wrap_content"/> <Button android:id="@+id/add_bill_btn" android:layout_width="wrap_content" android:layout_marginTop="20dp" android:background="@android:color/transparent" android:text="添加" android:layout_gravity="center" android:layout_height="wrap_content"/> </LinearLayout>