家庭小账本2.2
package com.example.famliyaccountbook; import androidx.annotation.RequiresPermission; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.view.*; import android.os.Bundle; import android.widget.*; import java.util.Calendar; public class WriteActivity extends AppCompatActivity { private DBManger manger; private EditText editText_date; private EditText editText_money; private Button button_outreport,back; private CheckBox r1,r2; private String time ,text2=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_write); findById(); time=getTime(); editText_date.setText(time); manger=new DBManger(this); button_outreport=findViewById(R.id.button_outreport); button_outreport.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //获取edittext数据 String text1 = editText_date.getText().toString().trim(); String text3 = editText_money.getText().toString().trim(); if(r1.isChecked()){ text2="收入"; }else if(r2.isChecked()){ text2 ="支出"; } manger.add(text1,text2,text3); Intent intent = new Intent(WriteActivity.this, MainActivity.class); finish(); startActivity(intent); } }); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(WriteActivity.this, MainActivity.class); finish(); startActivity(intent); } }); } //获取控件 private void findById(){ r1=findViewById(R.id.checkBox2);//收入 r2=findViewById(R.id.checkBox);//支出 editText_money=findViewById(R.id.editText_outmoney); editText_date=findViewById(R.id.editText); button_outreport=findViewById(R.id.button_outreport); back=findViewById(R.id.button_finish); } //获取时间 public String getTime(){ //获取年月日时分 Calendar calendar1 = Calendar.getInstance(); //年 int year = calendar1.get(Calendar.YEAR); //月 int month = calendar1.get(Calendar.MONTH) + 1; //日 int day = calendar1.get(Calendar.DAY_OF_MONTH); String result=year + "-" + month + "-" + day ; return result; } }
浏览账单(显示总金额)
ListActivity
package com.example.famliyaccountbook; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.*; public class ListActivity extends AppCompatActivity { private DBHelper helper=new DBHelper(this,1); private ListView list; private Button button_back; private EditText et_money,et_in,et_out; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); setTitle("浏览账本"); findById(); show_money(); information_show(); button_back=findViewById(R.id.button_back); button_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ListActivity.this, MainActivity.class); finish(); startActivity(intent); } }); } public void information_show(){ SQLiteDatabase db=helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from bill", null); String [] list1=new String[100]; int i=0; while (cursor.moveToNext()) { String newDate = cursor.getString(cursor.getColumnIndex("date")); String newType = cursor.getString(cursor.getColumnIndex("type")); String money = cursor.getString(cursor.getColumnIndex("money")); String str=newDate+" "+newType+" " +money; list1[i]=str; ++i; } cursor.close(); String []list2=new String[i]; int k=i; for(int j=0;j<i&&k>=0;j++){ --k; list2[k]=list1[j]; } ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list2); list.setAdapter(adapter); db.close(); } //获取控件 private void findById(){ list=findViewById(R.id.list_view); et_in=findViewById(R.id.in_money); et_out=findViewById(R.id.out_money); et_money=findViewById(R.id.sum_monry); } //获取金额 private double[] get_money(){ double i = 0; //记录总金额 double j = 0; //支出 double k = 0; //收入 double[] Arr = new double[3]; SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from bill where type = ? ", new String[]{"支出"}); while (cursor.moveToNext()) { String newMoney = cursor.getString(cursor.getColumnIndex("money")); if(newMoney.equals("")){ double t=0; j=j+t; }else { j= j+Double.parseDouble(newMoney); } } cursor.close(); db.close(); SQLiteDatabase db2 = helper.getReadableDatabase(); Cursor cursor2 = db2.rawQuery("select * from bill where type = ? ", new String[]{"收入"}); while (cursor2.moveToNext()) { String newMoney = cursor2.getString(cursor2.getColumnIndex("money")); if(newMoney.equals("")){ double t=0; k=k+t; }else { k= k+Double.parseDouble(newMoney); } } cursor2.close(); db2.close(); i=k-j; Arr[0]=i;Arr[1]=j;Arr[2]=k; return Arr; } //显示金额 private void show_money(){ double []result=get_money(); et_money.setText(result[0]+""); et_out.setText(result[1]+""); et_in.setText(result[2]+""); } }
activity_write.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/editText_time" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/activity" tools:context=".WriteActivity"> <TextView android:id="@+id/textView_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="47dp" android:layout_marginLeft="47dp" android:layout_marginTop="171dp" android:layout_marginBottom="73dp" android:text="日期" android:textSize="25dp" app:layout_constraintBottom_toTopOf="@+id/textView_type" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editText" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginStart="52dp" android:layout_marginLeft="52dp" android:layout_marginTop="158dp" android:layout_marginEnd="63dp" android:layout_marginRight="63dp" android:ems="10" android:inputType="textPersonName" android:textSize="25dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView_time" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="31dp" android:layout_marginLeft="31dp" android:layout_marginBottom="84dp" android:text="项目类型" android:textSize="25dp" app:layout_constraintBottom_toTopOf="@+id/textView5" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="38dp" android:layout_marginLeft="38dp" android:layout_marginBottom="301dp" android:text="金额" android:textSize="25dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" /> <EditText android:id="@+id/editText_outmoney" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginStart="45dp" android:layout_marginLeft="45dp" android:layout_marginEnd="79dp" android:layout_marginRight="79dp" android:layout_marginBottom="50dp" android:ems="10" android:hint="例如:100" android:inputType="number" android:textSize="25dp" app:layout_constraintBottom_toTopOf="@+id/button_outreport" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView5" /> <Button android:id="@+id/button_outreport" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginLeft="20dp" android:layout_marginEnd="162dp" android:layout_marginRight="162dp" android:layout_marginBottom="184dp" android:text="提交" android:textSize="25dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.546" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/button_finish" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="34dp" android:layout_marginLeft="34dp" android:layout_marginTop="50dp" android:layout_marginEnd="43dp" android:layout_marginRight="43dp" android:layout_marginBottom="184dp" android:text="返回主页面" android:textSize="25dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/button_outreport" app:layout_constraintTop_toBottomOf="@+id/editText_outmoney" /> <CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="22dp" android:layout_marginLeft="22dp" android:layout_marginTop="65dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="79dp" app:layout_constraintBottom_toTopOf="@+id/editText_outmoney" app:layout_constraintEnd_toStartOf="@+id/textView2" app:layout_constraintStart_toEndOf="@+id/textView_type" app:layout_constraintTop_toBottomOf="@+id/editText"></CheckBox> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="63dp" android:layout_marginEnd="31dp" android:layout_marginRight="31dp" android:layout_marginBottom="79dp" android:text="支出" android:textSize="25dp" app:layout_constraintBottom_toTopOf="@+id/editText_outmoney" app:layout_constraintEnd_toStartOf="@+id/checkBox2" app:layout_constraintStart_toEndOf="@+id/checkBox" app:layout_constraintTop_toBottomOf="@+id/editText" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="2dp" android:layout_marginLeft="2dp" android:layout_marginTop="65dp" android:layout_marginEnd="7dp" android:layout_marginRight="7dp" android:layout_marginBottom="79dp" app:layout_constraintBottom_toTopOf="@+id/editText_outmoney" app:layout_constraintEnd_toStartOf="@+id/textView3" app:layout_constraintStart_toEndOf="@+id/textView2" app:layout_constraintTop_toBottomOf="@+id/editText" app:layout_constraintVertical_bias="0.0"></CheckBox> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="5dp" android:layout_marginLeft="5dp" android:layout_marginTop="65dp" android:layout_marginEnd="43dp" android:layout_marginRight="43dp" android:layout_marginBottom="77dp" android:text="收入" android:textSize="25dp" app:layout_constraintBottom_toTopOf="@+id/editText_outmoney" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/checkBox2" app:layout_constraintTop_toBottomOf="@+id/editText" /> </androidx.constraintlayout.widget.ConstraintLayout>
activity_list.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/activity" tools:context=".ListActivity"> <Button android:id="@+id/button_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="295dp" android:layout_marginLeft="295dp" android:layout_marginEnd="22dp" android:layout_marginRight="22dp" android:layout_marginBottom="110dp" android:text="返回主页面" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/textshow_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="65dp" android:layout_marginLeft="65dp" android:layout_marginTop="27dp" android:layout_marginEnd="74dp" android:layout_marginRight="74dp" android:layout_marginBottom="17dp" android:text="日期" app:layout_constraintBottom_toTopOf="@+id/list_view" app:layout_constraintEnd_toStartOf="@+id/textshow_type" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/out_money" /> <TextView android:id="@+id/textshow_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="74dp" android:layout_marginLeft="74dp" android:layout_marginTop="27dp" android:layout_marginBottom="17dp" android:text="类型" app:layout_constraintBottom_toTopOf="@+id/list_view" app:layout_constraintStart_toEndOf="@+id/textshow_date" app:layout_constraintTop_toBottomOf="@+id/out_money" /> <TextView android:id="@+id/textshow_money" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="214dp" android:layout_marginEnd="95dp" android:layout_marginRight="95dp" android:layout_marginBottom="17dp" android:text="金额" app:layout_constraintBottom_toTopOf="@+id/list_view" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="37dp" android:layout_marginLeft="37dp" android:layout_marginTop="52dp" android:layout_marginBottom="35dp" android:text="总金额" app:layout_constraintBottom_toTopOf="@+id/textView6" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="37dp" android:layout_marginLeft="37dp" android:layout_marginBottom="1dp" android:text="收入" app:layout_constraintBottom_toTopOf="@+id/textView7" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView4" /> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="37dp" android:layout_marginLeft="37dp" android:layout_marginTop="37dp" android:layout_marginEnd="15dp" android:layout_marginRight="15dp" android:layout_marginBottom="70dp" android:text="支出" app:layout_constraintBottom_toTopOf="@+id/list_view" app:layout_constraintEnd_toStartOf="@+id/out_money" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView6" /> <EditText android:id="@+id/sum_monry" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginStart="15dp" android:layout_marginLeft="15dp" android:layout_marginTop="38dp" android:layout_marginEnd="217dp" android:layout_marginRight="217dp" android:layout_marginBottom="6dp" android:ems="10" android:inputType="textPersonName" app:layout_constraintBottom_toTopOf="@+id/in_money" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView4" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/in_money" android:layout_width="115dp" android:layout_height="wrap_content" android:layout_marginStart="14dp" android:layout_marginLeft="14dp" android:layout_marginTop="6dp" android:layout_marginEnd="217dp" android:layout_marginRight="217dp" android:layout_marginBottom="5dp" android:ems="10" android:inputType="textPersonName" app:layout_constraintBottom_toTopOf="@+id/out_money" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/textView6" app:layout_constraintTop_toBottomOf="@+id/sum_monry" /> <EditText android:id="@+id/out_money" android:layout_width="115dp" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:layout_marginEnd="216dp" android:layout_marginRight="216dp" android:layout_marginBottom="2dp" android:ems="10" android:inputType="textPersonName" app:layout_constraintBottom_toTopOf="@+id/textshow_date" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/in_money" /> <ListView android:id="@+id/list_view" android:layout_width="340dp" android:layout_height="300dp" android:layout_marginStart="37dp" android:layout_marginLeft="37dp" android:layout_marginEnd="34dp" android:layout_marginRight="34dp" android:layout_marginBottom="22dp" app:layout_constraintBottom_toTopOf="@+id/button_back" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"></ListView> </androidx.constraintlayout.widget.ConstraintLayout>