今日完成了账本开发的统计功能,输入开始的月份与结束的月份,点击计算按扭就可以统计总支出、总收入和结余。
到此账本开发已经全部完成实现了,查询今天,当月,当年以及特定的日期范围内的收入、支出等。
添加账目,长按删除账目,统计特定日期范围内的总支出,总收入以及结余。下面为统计功能的代码部分及全部功能的截图:
<?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"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="right"> <Button android:id="@+id/btnCalculate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="计算" android:textAllCaps="false"> </Button> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="从(年—月—日)" android:layout_marginTop="25dp"> </TextView> <EditText android:id="@+id/txtReportFrom" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="date"> </EditText> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="到(年—月—日)" android:layout_marginTop="25dp"> </TextView> <EditText android:id="@+id/txtReportTo" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="date"> </EditText> <TextView android:id="@+id/txtReportIncome" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp"> </TextView> <TextView android:id="@+id/txtReportExpenditure" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp"> </TextView> <TextView android:id="@+id/txtReportSurplus" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp"> </TextView> </LinearLayout>
public class ReportActivity extends AppCompatActivity implements View.OnClickListener,View.OnLongClickListener{ private EditText txtFrom; private EditText txtTo; private TextView txtExpenditure; private TextView txtIncome; private TextView txtSurplus; private Button btnCalculate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.report_layout); setTitle("统计"); CDateTime dt=new CDateTime(); txtFrom=(EditText)findViewById(R.id.txtReportFrom); txtFrom.setOnLongClickListener(this); txtFrom.setText(dt.toDateString()); txtTo=(EditText)findViewById(R.id.txtReportTo); txtTo.setOnLongClickListener(this); txtTo.setText(dt.toDateString()); txtExpenditure=(TextView)findViewById(R.id.txtReportExpenditure); txtIncome=(TextView)findViewById(R.id.txtReportIncome); txtSurplus=(TextView)findViewById(R.id.txtReportSurplus); btnCalculate=(Button)findViewById(R.id.btnCalculate); btnCalculate.setOnClickListener(this); } @Override public void onClick(View v) { if(v.getId()==R.id.btnCalculate){ //整理日期 long startTime=0L,endTime=0L; String sFrom=txtFrom.getText().toString().trim(); String sTo=txtTo.getText().toString().trim(); if(CDateTime.checkDateString(sFrom,"-")&&CDateTime.checkDateString(sTo,"-")){ startTime=new CDateTime(sFrom,"-").startOfDay(); endTime=new CDateTime(sTo,"-").endOfDay(); } //显示计算结果 CAccount acct=CAccount.getInstance(this,1); double incomeSum=acct.incomeSum(startTime,endTime); double expenditureSum=acct.expenditureSum(startTime,endTime); txtIncome.setText(String.format("%s:%.2f","总收入",incomeSum)); txtExpenditure.setText(String.format("%s:%.2f","总支出",expenditureSum)); txtSurplus.setText(String.format("%s:%.2f","结余",incomeSum-expenditureSum)); } } @Override public boolean onLongClick(View v) { int vid=v.getId(); if(vid==R.id.txtReportFrom){ CDateTime dt=new CDateTime(); DatePickerDialog dlg=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { txtFrom.setText(String.format("%d-%d-%d",year,month+1,dayOfMonth)); } },dt.year(),dt.month()-1,dt.day()); dlg.show(); }else if(vid==R.id.txtReportTo){ CDateTime dt=new CDateTime(); DatePickerDialog dlg=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { txtTo.setText(String.format("%d-%d-%d",year,month+1,dayOfMonth)); } },dt.year(),dt.month()-1,dt.day()); dlg.show(); } return false; } }