记录数据,将数据放进记账本
package com.example.bookkeeping; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import java.text.SimpleDateFormat; import java.util.Date; public class jilu extends AppCompatActivity { private MyDatabaseHelper dbHelper; private EditText editText1; private EditText editText2; private EditText editText3; String shouru=""; String zhichu=""; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 ");// HH:mm:ss //获取当前时间 Date date = new Date(System.currentTimeMillis()); protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_jilu); dbHelper=new MyDatabaseHelper(this,"zhangben.db",null,4); dbHelper.getWritableDatabase(); editText1 = (EditText) findViewById(R.id.edit_11); editText2 = (EditText) findViewById(R.id.edit_12); editText3 = (EditText) findViewById(R.id.edit_13); final RadioButton sex1=(RadioButton) findViewById(R.id.radioButton1);//获取单选按钮组 final RadioButton sex2=(RadioButton) findViewById(R.id.radioButton2);//获取单选按钮组 Button button=(Button)findViewById(R.id.button); editText3=(EditText)findViewById(R.id.edit_13); editText3.setText(simpleDateFormat.format(date)); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db=dbHelper.getWritableDatabase(); ContentValues values=new ContentValues(); String m1=editText1.getText().toString(); String m2=editText2.getText().toString(); String m3=editText3.getText().toString(); values.put("原因",m1); values.put("金额",m2); values.put("时间",m3); if(sex1.isChecked()){ shouru="收入"; values.put("盈亏",shouru); } if(sex2.isChecked()){ zhichu="支出"; values.put("盈亏",zhichu);//存性别 } if(m1.equals("")||m2.equals("")||m3.equals("")) { Toast.makeText(jilu.this, "信息不全,请补充", Toast.LENGTH_SHORT).show(); } else{ db.insert("zhangben",null,values); //注意别漏掉 values.clear(); Toast.makeText(jilu.this, "保存成功", Toast.LENGTH_SHORT).show(); editText1.setText("");//保存成功清空学号和姓名信息 editText2.setText(""); editText3.setText(simpleDateFormat.format(date)); } } }); } }